← back to elixir-nx__nx

Function bodies 219 total

All specs Real LLM only Function bodies
svd_2 function · cpp · L1045-L1066 (22 LOC)
torchx/c_src/torchx.cpp
fine::Ok<
    std::tuple<fine::ResourcePtr<TorchTensor>, fine::ResourcePtr<TorchTensor>,
               fine::ResourcePtr<TorchTensor>>>
svd_2(ErlNifEnv *env, fine::ResourcePtr<TorchTensor> t, bool full_matrices) {
  try {
    auto result = torch::linalg_svd(get_tensor(t), full_matrices);
    return fine::Ok(
        std::make_tuple(fine::make_resource<TorchTensor>(std::get<0>(result)),
                        fine::make_resource<TorchTensor>(std::get<1>(result)),
                        fine::make_resource<TorchTensor>(std::get<2>(result))));
  } catch (const c10::Error &e) {
    throw std::runtime_error(std::string("SVD decomposition") +
                             " failed: " + e.what());
  } catch (const std::exception &e) {
    throw std::runtime_error(std::string("SVD decomposition") +
                             " failed: " + e.what());
  } catch (...) {
    throw std::runtime_error(std::string("SVD decomposition") +
                             " failed with unknown error");
lu function · cpp · L1070-L1094 (25 LOC)
torchx/c_src/torchx.cpp
fine::Ok<
    std::tuple<fine::ResourcePtr<TorchTensor>, fine::ResourcePtr<TorchTensor>,
               fine::ResourcePtr<TorchTensor>>>
lu(ErlNifEnv *env, fine::ResourcePtr<TorchTensor> t) {
  try {
    std::tuple<torch::Tensor, torch::Tensor> lu_result =
        torch::linalg_lu_factor(get_tensor(t));
    std::tuple<torch::Tensor, torch::Tensor, torch::Tensor> plu =
        torch::lu_unpack(std::get<0>(lu_result), std::get<1>(lu_result));
    return fine::Ok(
        std::make_tuple(fine::make_resource<TorchTensor>(std::get<0>(plu)),
                        fine::make_resource<TorchTensor>(std::get<1>(plu)),
                        fine::make_resource<TorchTensor>(std::get<2>(plu))));
  } catch (const c10::Error &e) {
    throw std::runtime_error(std::string("LU decomposition") +
                             " failed: " + e.what());
  } catch (const std::exception &e) {
    throw std::runtime_error(std::string("LU decomposition") +
                             " failed: " + e.what()
amax function · cpp · L1097-L1103 (7 LOC)
torchx/c_src/torchx.cpp
fine::Ok<fine::ResourcePtr<TorchTensor>>
amax(ErlNifEnv *env, fine::ResourcePtr<TorchTensor> tensor,
     std::vector<int64_t> axes, bool keep_axes) {
  return tensor_ok(
      at::amax(get_tensor(tensor), vec_to_array_ref(axes), keep_axes));
}
amin function · cpp · L1106-L1112 (7 LOC)
torchx/c_src/torchx.cpp
fine::Ok<fine::ResourcePtr<TorchTensor>>
amin(ErlNifEnv *env, fine::ResourcePtr<TorchTensor> tensor,
     std::vector<int64_t> axes, bool keep_axes) {
  return tensor_ok(
      at::amin(get_tensor(tensor), vec_to_array_ref(axes), keep_axes));
}
eigh function · cpp · L1115-L1134 (20 LOC)
torchx/c_src/torchx.cpp
fine::Ok<
    std::tuple<fine::ResourcePtr<TorchTensor>, fine::ResourcePtr<TorchTensor>>>
eigh(ErlNifEnv *env, fine::ResourcePtr<TorchTensor> tensor) {
  try {
    auto result = torch::linalg_eigh(get_tensor(tensor));
    return fine::Ok(
        std::make_tuple(fine::make_resource<TorchTensor>(std::get<0>(result)),
                        fine::make_resource<TorchTensor>(std::get<1>(result))));
  } catch (const c10::Error &e) {
    throw std::runtime_error(std::string("Eigenvalue decomposition (eigh)") +
                             " failed: " + e.what());
  } catch (const std::exception &e) {
    throw std::runtime_error(std::string("Eigenvalue decomposition (eigh)") +
                             " failed: " + e.what());
  } catch (...) {
    throw std::runtime_error(std::string("Eigenvalue decomposition (eigh)") +
                             " failed with unknown error");
  }
}
solve function · cpp · L1137-L1144 (8 LOC)
torchx/c_src/torchx.cpp
fine::Ok<fine::ResourcePtr<TorchTensor>>
solve(ErlNifEnv *env, fine::ResourcePtr<TorchTensor> tensorA,
      fine::ResourcePtr<TorchTensor> tensorB) {
  TORCH_CATCH_ERROR(
      tensor_ok(torch::linalg_solve(get_tensor(tensorA), get_tensor(tensorB))),
      "Linear solve");
}
conv function · cpp · L1147-L1162 (16 LOC)
torchx/c_src/torchx.cpp
fine::Ok<fine::ResourcePtr<TorchTensor>>
conv(ErlNifEnv *env, fine::ResourcePtr<TorchTensor> tensor,
     fine::ResourcePtr<TorchTensor> kernel, std::vector<int64_t> stride,
     std::vector<int64_t> padding, std::vector<int64_t> dilation,
     bool transposed, int64_t groups) {
  c10::optional<at::Tensor> bias_tensor;
  std::vector<int64_t> output_padding;
  output_padding.push_back(0);

  return tensor_ok(at::convolution(get_tensor(tensor), get_tensor(kernel),
                                   bias_tensor, vec_to_array_ref(stride),
                                   vec_to_array_ref(padding),
                                   vec_to_array_ref(dilation), transposed,
                                   vec_to_array_ref(output_padding), groups));
}
Repobility · open methodology · https://repobility.com/research/
max_pool_3d function · cpp · L1165-L1174 (10 LOC)
torchx/c_src/torchx.cpp
fine::Ok<fine::ResourcePtr<TorchTensor>>
max_pool_3d(ErlNifEnv *env, fine::ResourcePtr<TorchTensor> tensor,
            std::vector<int64_t> kernel_size, std::vector<int64_t> strides,
            std::vector<int64_t> padding, std::vector<int64_t> dilation) {
  return tensor_ok(
      at::max_pool3d(get_tensor(tensor), vec_to_array_ref(kernel_size),
                     vec_to_array_ref(strides), vec_to_array_ref(padding),
                     vec_to_array_ref(dilation)));
}
string2type function · c · L47-L50 (4 LOC)
torchx/c_src/torchx_nif_util.h
inline torch::ScalarType string2type(const std::string &atom) {
  return dtypes[atom];
}
TorchTensor class · c · L63-L98 (36 LOC)
torchx/c_src/torchx_nif_util.h
class TorchTensor {
public:
  TorchTensor(torch::Tensor tensor) : tensor_(tensor), deallocated_(false) {}

  torch::Tensor &tensor() {
    if (deallocated_) {
      throw std::runtime_error("Tensor has been deallocated");
    }
    return tensor_;
  }

  const torch::Tensor &tensor() const {
    if (deallocated_) {
      throw std::runtime_error("Tensor has been deallocated");
    }
    return tensor_;
  }

  bool deallocate() {
    if (!deallocated_) {
      deallocated_ = true;
      // Assignment to empty tensor properly handles destruction and frees
      // memory The destructor will be called automatically by the assignment
      // operator
      tensor_ = torch::Tensor();
      return true;
    }
    return false;
  }

  bool is_deallocated() const { return deallocated_; }

private:
  torch::Tensor tensor_;
  bool deallocated_;
};
deallocate method · c · L80-L91 (12 LOC)
torchx/c_src/torchx_nif_util.h
  bool deallocate() {
    if (!deallocated_) {
      deallocated_ = true;
      // Assignment to empty tensor properly handles destruction and frees
      // memory The destructor will be called automatically by the assignment
      // operator
      tensor_ = torch::Tensor();
      return true;
    }
    return false;
  }
decode function · c · L108-L139 (32 LOC)
torchx/c_src/torchx_nif_util.h
  static std::vector<int64_t> decode(ErlNifEnv *env, const ERL_NIF_TERM &term) {
    // First try to decode as tuple (for shapes)
    int size;
    const ERL_NIF_TERM *terms;
    if (enif_get_tuple(env, term, &size, &terms)) {
      std::vector<int64_t> vec;
      vec.reserve(size);
      for (int i = 0; i < size; i++) {
        vec.push_back(fine::decode<int64_t>(env, terms[i]));
      }
      return vec;
    }

    // Otherwise try to decode as list
    unsigned int length;
    if (!enif_get_list_length(env, term, &length)) {
      throw std::invalid_argument("decode failed, expected a tuple or list");
    }

    std::vector<int64_t> vector;
    vector.reserve(length);

    auto list = term;
    ERL_NIF_TERM head, tail;
    while (enif_get_list_cell(env, list, &head, &tail)) {
      auto elem = fine::decode<int64_t>(env, head);
      vector.push_back(elem);
      list = tail;
    }

    return vector;
  }
decode function · c · L144-L159 (16 LOC)
torchx/c_src/torchx_nif_util.h
  static torch::Scalar decode(ErlNifEnv *env, const ERL_NIF_TERM &term) {
    // Try to decode as double
    try {
      return torch::Scalar(fine::decode<double>(env, term));
    } catch (const std::invalid_argument &) {
      // Try to decode as int64
      try {
        return torch::Scalar(fine::decode<int64_t>(env, term));
      } catch (const std::invalid_argument &) {
        // Try to decode as complex number (tuple of two doubles)
        auto complex_tuple = fine::decode<std::tuple<double, double>>(env, term);
        return torch::Scalar(c10::complex<double>(std::get<0>(complex_tuple),
                                                   std::get<1>(complex_tuple)));
      }
    }
  }
encode function · c · L164-L175 (12 LOC)
torchx/c_src/torchx_nif_util.h
  static ERL_NIF_TERM encode(ErlNifEnv *env, const torch::Scalar &scalar) {
    if (scalar.isIntegral(false)) {
      return fine::encode(env, scalar.toLong());
    } else if (scalar.isFloatingPoint()) {
      return fine::encode(env, scalar.toDouble());
    } else if (scalar.isComplex()) {
      auto complex = scalar.toComplexDouble();
      return fine::encode(env, std::make_tuple(complex.real(), complex.imag()));
    } else {
      throw std::runtime_error("Unknown scalar type");
    }
  }
decode function · c · L180-L183 (4 LOC)
torchx/c_src/torchx_nif_util.h
  static torch::ScalarType decode(ErlNifEnv *env, const ERL_NIF_TERM &term) {
    auto type_string = fine::decode<fine::Atom>(env, term).to_string();
    return torchx::string2type(type_string);
  }
Generated by Repobility's multi-pass static-analysis pipeline (https://repobility.com)
decode function · c · L188-L191 (4 LOC)
torchx/c_src/torchx_nif_util.h
  static torch::Device decode(ErlNifEnv *env, const ERL_NIF_TERM &term) {
    auto device_string = fine::decode<std::string>(env, term);
    return torch::Device(device_string);
  }
decode function · c · L196-L201 (6 LOC)
torchx/c_src/torchx_nif_util.h
  static c10::IntArrayRef decode(ErlNifEnv *env, const ERL_NIF_TERM &term) {
    // We need to store the vector somewhere persistent for IntArrayRef to reference
    // This is tricky - IntArrayRef is a view, so we'll decode to vector instead
    throw std::runtime_error(
        "Cannot decode directly to IntArrayRef, use std::vector<int64_t>");
  }
decode function · c · L207-L216 (10 LOC)
torchx/c_src/torchx_nif_util.h
  static std::vector<torch::Tensor> decode(ErlNifEnv *env,
                                           const ERL_NIF_TERM &term) {
    auto tensor_resources = fine::decode<std::vector<fine::ResourcePtr<torchx::TorchTensor>>>(env, term);
    std::vector<torch::Tensor> tensors;
    tensors.reserve(tensor_resources.size());
    for (const auto &res : tensor_resources) {
      tensors.push_back(res->tensor());
    }
    return tensors;
  }
encode function · c · L222-L230 (9 LOC)
torchx/c_src/torchx_nif_util.h
  static ERL_NIF_TERM encode(ErlNifEnv *env,
                             const std::vector<torch::Tensor> &tensors) {
    std::vector<fine::ResourcePtr<torchx::TorchTensor>> tensor_resources;
    tensor_resources.reserve(tensors.size());
    for (const auto &tensor : tensors) {
      tensor_resources.push_back(fine::make_resource<torchx::TorchTensor>(tensor));
    }
    return fine::encode(env, tensor_resources);
  }
‹ prevpage 5 / 5