ext/torch/ext.cpp in torch-rb-0.1.5 vs ext/torch/ext.cpp in torch-rb-0.1.6
- old
+ new
@@ -14,17 +14,10 @@
#include "tensor_functions.hpp"
#include "nn_functions.hpp"
using namespace Rice;
-Object tensor_array(std::tuple<torch::Tensor, torch::Tensor> x) {
- Array a;
- a.push(to_ruby<torch::Tensor>(std::get<0>(x)));
- a.push(to_ruby<torch::Tensor>(std::get<1>(x)));
- return Object(a);
-}
-
extern "C"
void Init_ext()
{
Module rb_mTorch = define_module("Torch");
add_torch_functions(rb_mTorch);
@@ -111,97 +104,14 @@
*[](IntArrayRef size, const torch::TensorOptions &options) {
return torch::zeros(size, options);
})
// begin operations
.define_singleton_method(
- "_mean",
- *[](Tensor& input) {
- return torch::mean(input);
- })
- .define_singleton_method(
- "_mean_dim",
- *[](Tensor& input, int64_t dim, bool keepdim) {
- return torch::mean(input, dim, keepdim);
- })
- .define_singleton_method(
- "_sum",
- *[](Tensor& input) {
- return torch::sum(input);
- })
- .define_singleton_method(
- "_sum_dim",
- *[](Tensor& input, int64_t dim, bool keepdim) {
- return torch::sum(input, dim, keepdim);
- })
- .define_singleton_method(
- "_max_out",
- *[](Tensor &max, Tensor &max_indices, const Tensor &input, int64_t dim, bool keepdim) {
- return tensor_array(torch::_max_out(max, max_indices, input, dim, keepdim));
- })
- .define_singleton_method(
- "_topk",
- *[](Tensor& input, int64_t k) {
- return tensor_array(torch::topk(input, k));
- })
- .define_singleton_method(
- "_softmax",
- *[](const Tensor &input, int64_t dim) {
- return torch::softmax(input, dim);
- })
- .define_singleton_method(
- "_log_softmax",
- *[](Tensor& input, int64_t dim) {
- return torch::log_softmax(input, dim);
- })
- .define_singleton_method(
- "relu",
- *[](Tensor& input) {
- return torch::relu(input);
- })
- .define_singleton_method(
- "prelu",
- *[](torch::Tensor& input, torch::Tensor& weight) {
- return torch::prelu(input, weight);
- })
- .define_singleton_method(
- "leaky_relu",
- *[](torch::Tensor& input, Scalar negative_slope) {
- return torch::leaky_relu(input, negative_slope);
- })
- .define_singleton_method(
- "conv2d",
- *[](Tensor& input, Tensor& weight, Tensor& bias, IntArrayRef stride, IntArrayRef padding, IntArrayRef dilation, int64_t groups) {
- return torch::conv2d(input, weight, bias, stride, padding, dilation, groups);
- })
- // linear layers
- .define_singleton_method(
- "bilinear",
- *[](const Tensor &input1, const Tensor &input2, const Tensor &weight, const Tensor &bias) {
- return torch::bilinear(input1, input2, weight, bias);
- })
- .define_singleton_method(
- "linear",
- *[](Tensor& input, Tensor& weight, Tensor& bias) {
- return torch::linear(input, weight, bias);
- })
- // pooling layers
- .define_singleton_method(
- "max_pool2d",
- *[](Tensor& input, IntArrayRef kernel_size) {
- return torch::max_pool2d(input, kernel_size);
- })
- .define_singleton_method(
- "avg_pool2d",
- *[](Tensor& input, IntArrayRef kernel_size) {
- return torch::avg_pool2d(input, kernel_size);
- })
- .define_singleton_method(
"_binary_cross_entropy_with_logits",
*[](const Tensor &input, const Tensor &target, OptionalTensor weight, OptionalTensor pos_weight, MyReduction reduction) {
return torch::binary_cross_entropy_with_logits(input, target, weight, pos_weight, reduction);
})
- .define_singleton_method("numel", &torch::numel)
.define_singleton_method(
"_from_blob",
*[](String s, IntArrayRef size, const torch::TensorOptions &options) {
void *data = const_cast<char *>(s.c_str());
return torch::from_blob(data, size, options);
@@ -224,20 +134,15 @@
return t.reshape(size);
});
rb_cTensor
.define_method("cuda?", &torch::Tensor::is_cuda)
- .define_method("distributed?", &torch::Tensor::is_distributed)
- .define_method("complex?", &torch::Tensor::is_complex)
- .define_method("floating_point?", &torch::Tensor::is_floating_point)
- .define_method("signed?", &torch::Tensor::is_signed)
.define_method("sparse?", &torch::Tensor::is_sparse)
.define_method("quantized?", &torch::Tensor::is_quantized)
.define_method("dim", &torch::Tensor::dim)
.define_method("element_size", &torch::Tensor::element_size)
.define_method("requires_grad", &torch::Tensor::requires_grad)
- .define_method("view_as", &torch::Tensor::view_as)
.define_method(
"addcmul!",
*[](Tensor& self, Scalar value, const Tensor & tensor1, const Tensor & tensor2) {
return self.addcmul_(tensor1, tensor2, value);
})
@@ -245,25 +150,10 @@
"addcdiv!",
*[](Tensor& self, Scalar value, const Tensor & tensor1, const Tensor & tensor2) {
return self.addcdiv_(tensor1, tensor2, value);
})
.define_method(
- "zero!",
- *[](Tensor& self) {
- return self.zero_();
- })
- .define_method(
- "detach",
- *[](Tensor& self) {
- return self.detach();
- })
- .define_method(
- "detach!",
- *[](Tensor& self) {
- return self.detach_();
- })
- .define_method(
"_requires_grad!",
*[](Tensor& self, bool requires_grad) {
return self.set_requires_grad(requires_grad);
})
.define_method(
@@ -297,69 +187,9 @@
"device",
*[](Tensor& self) {
std::stringstream s;
s << self.device();
return s.str();
- })
- .define_method(
- "resize_as!",
- *[](Tensor& self, Tensor& other) {
- return self.resize_as_(other);
- })
- .define_method(
- "fill!",
- *[](Tensor& self, Scalar value) {
- return self.fill_(value);
- })
- .define_method(
- "relu!",
- *[](Tensor& self) {
- return self.relu_();
- })
- .define_method(
- "normal!",
- *[](Tensor& self, double mean, double std) {
- return self.normal_(mean, std);
- })
- .define_method(
- "random!",
- *[](Tensor& self, int64_t to) {
- return self.random_(to);
- })
- .define_method(
- "sub!",
- *[](Tensor& self, Tensor& other) {
- return self.sub_(other);
- })
- .define_method(
- "div!",
- *[](Tensor& self, Tensor& other) {
- return self.div_(other);
- })
- .define_method(
- "sqrt!",
- *[](Tensor& self) {
- return self.sqrt_();
- })
- .define_method(
- "unsqueeze!",
- *[](Tensor& self, int64_t dim) {
- return self.unsqueeze_(dim);
- })
- .define_method(
- "copy!",
- *[](Tensor& self, Tensor& src) {
- return self.copy_(src);
- })
- .define_method(
- "clone",
- *[](Tensor& self) {
- return self.clone();
- })
- .define_method(
- "data",
- *[](Tensor& self) {
- return self.data();
})
.define_method(
"_flat_data",
*[](Tensor& self) {
Array a;