diff --git a/.gitignore b/.gitignore index 82b31df8..ad96cbd7 100644 --- a/.gitignore +++ b/.gitignore @@ -335,3 +335,8 @@ ASALocalRun/ /tensorflowlib/linux/native/libtensorflow.so /src/TensorFlowNET.Core/tensorflow.dll /docs/build +src/TensorFlowNET.Native/libtensorflow.dll +src/TensorFlowNET.Native/bazel-* +/src/TensorFlowNET.Native/libtensorflow.lib +src/TensorFlowNET.Native/c_api.h +/.vscode diff --git a/src/TensorFlowNET.Native/BUILD b/src/TensorFlowNET.Native/BUILD new file mode 100644 index 00000000..8a581543 --- /dev/null +++ b/src/TensorFlowNET.Native/BUILD @@ -0,0 +1,19 @@ +# Description: +# CSharp Native Interface library intended for implementing the +# TensorFlow .NET Standard API using the TensorFlow C library. + +licenses(["notice"]) # Apache 2.0 + +cc_import( + name = "libtensorflow", + hdrs = ["c_api.h"], + interface_library = "libtensorflow.lib", + shared_library = "libtensorflow.dll", +) + +cc_binary( + name = "csni", + srcs = ["csni.cc"], + deps = [":libtensorflow"], + linkstatic = 0 +) diff --git a/src/TensorFlowNET.Native/WORKSPACE b/src/TensorFlowNET.Native/WORKSPACE new file mode 100644 index 00000000..e69de29b diff --git a/src/TensorFlowNET.Native/csni.cc b/src/TensorFlowNET.Native/csni.cc new file mode 100644 index 00000000..40dfe7dc --- /dev/null +++ b/src/TensorFlowNET.Native/csni.cc @@ -0,0 +1,21 @@ +#include +#include +#include "c_api.h" +typedef char* (__stdcall *TFFunc)(); + +int main() { + HINSTANCE hinstLib = LoadLibrary(TEXT("libtensorflow.dll")); + if (!hinstLib) { + std::cout << "could not load the dynamic library" << std::endl; + return EXIT_FAILURE; + } + + TFFunc version = (TFFunc) GetProcAddress(hinstLib, "TF_Version"); + if (!version) { + std::cout << "could not locate the function" << std::endl; + return EXIT_FAILURE; + } + + printf("Hello from TensorFlow C library version %s", version()); + return 0; +} \ No newline at end of file diff --git a/src/tensorflow/README.md b/src/tensorflow/README.md new file mode 100644 index 00000000..d3ae1e81 --- /dev/null +++ b/src/tensorflow/README.md @@ -0,0 +1,12 @@ +### How to compile CSharp Native Interface + + +git clone https://github.com/tensorflow/tensorflow + +`cd tensorflow/tensorflow` + +copy `csharp` folder to `tensorflow`, the csharp folder should be in the same parent directory with other language binding. + +`cd csharp` + +`bazel build //tensorflow/csharp:csni` \ No newline at end of file diff --git a/src/tensorflow/csharp/BUILD b/src/tensorflow/csharp/BUILD new file mode 100644 index 00000000..bcb502d5 --- /dev/null +++ b/src/tensorflow/csharp/BUILD @@ -0,0 +1,23 @@ +# Description: +# CSharp Native Interface library intended for implementing the +# TensorFlow .NET Standard API using the TensorFlow C library. +package( + default_visibility = ["//visibility:private"], +) + +licenses(["notice"]) # Apache 2.0 + +load( + "//tensorflow:tensorflow.bzl", + "tf_cc_binary" +) + + + +tf_cc_binary( + name = "csni", + srcs = ["csni.cc"], + deps = [ + "//tensorflow/c:c_api", + "//tensorflow/csharp/eager:cswrap_tfe_lib"], +) diff --git a/src/tensorflow/csharp/csni.cc b/src/tensorflow/csharp/csni.cc new file mode 100644 index 00000000..a7f08f2f --- /dev/null +++ b/src/tensorflow/csharp/csni.cc @@ -0,0 +1,7 @@ +#include +#include "tensorflow/c/c_api.h" + +int main() { + printf("Hello from TensorFlow C library version %s", TF_Version()); + return 0; +} \ No newline at end of file diff --git a/src/tensorflow/csharp/eager/BUILD b/src/tensorflow/csharp/eager/BUILD new file mode 100644 index 00000000..a44d69cf --- /dev/null +++ b/src/tensorflow/csharp/eager/BUILD @@ -0,0 +1,19 @@ +load("//tensorflow:tensorflow.bzl", "tf_cc_binary") + +cc_library( + name = "cswrap_tfe_lib", + srcs = [ + "cswrap_tfe_src.cc", + ], + hdrs = [ + "cswrap_tfe.h", + ], + visibility = [ + "//learning/deepmind/courier:__subpackages__", + "//tensorflow:internal", + ], + deps = [ + "//tensorflow/c:c_api", + + ], +) \ No newline at end of file diff --git a/src/tensorflow/csharp/eager/cswrap_tfe.h b/src/tensorflow/csharp/eager/cswrap_tfe.h new file mode 100644 index 00000000..a2901f3c --- /dev/null +++ b/src/tensorflow/csharp/eager/cswrap_tfe.h @@ -0,0 +1,4 @@ +// Record the gradient for a given op. +extern void TFE_Py_RecordGradient(const char* op_name, void* inputs, + void* attrs, void* results, + const char* name); \ No newline at end of file diff --git a/src/tensorflow/csharp/eager/cswrap_tfe_src.cc b/src/tensorflow/csharp/eager/cswrap_tfe_src.cc new file mode 100644 index 00000000..6fbeaf03 --- /dev/null +++ b/src/tensorflow/csharp/eager/cswrap_tfe_src.cc @@ -0,0 +1,23 @@ +#include +#include + +#include "tensorflow/core/lib/core/errors.h" +#include "cswrap_tfe.h" + +#include "tensorflow/c/c_api.h" +#include "tensorflow/c/c_api_internal.h" + +#include "tensorflow/core/platform/protobuf.h" +#include "tensorflow/core/platform/types.h" + + +namespace { + +void RecordGradient(const char* op_name, void* inputs, + void* attrs, void* results, + const char* name) { + // std::vector input_ids = MakeTensorIDList(inputs); + +} + +} \ No newline at end of file