Browse Source

add CSharp Native Interface in C++

tags/v0.1.0-Tensor
Oceania2018 6 years ago
parent
commit
fd789d49bd
10 changed files with 133 additions and 0 deletions
  1. +5
    -0
      .gitignore
  2. +19
    -0
      src/TensorFlowNET.Native/BUILD
  3. +0
    -0
      src/TensorFlowNET.Native/WORKSPACE
  4. +21
    -0
      src/TensorFlowNET.Native/csni.cc
  5. +12
    -0
      src/tensorflow/README.md
  6. +23
    -0
      src/tensorflow/csharp/BUILD
  7. +7
    -0
      src/tensorflow/csharp/csni.cc
  8. +19
    -0
      src/tensorflow/csharp/eager/BUILD
  9. +4
    -0
      src/tensorflow/csharp/eager/cswrap_tfe.h
  10. +23
    -0
      src/tensorflow/csharp/eager/cswrap_tfe_src.cc

+ 5
- 0
.gitignore View File

@@ -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

+ 19
- 0
src/TensorFlowNET.Native/BUILD View File

@@ -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
)

+ 0
- 0
src/TensorFlowNET.Native/WORKSPACE View File


+ 21
- 0
src/TensorFlowNET.Native/csni.cc View File

@@ -0,0 +1,21 @@
#include <windows.h>
#include <iostream>
#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;
}

+ 12
- 0
src/tensorflow/README.md View File

@@ -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`

+ 23
- 0
src/tensorflow/csharp/BUILD View File

@@ -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"],
)

+ 7
- 0
src/tensorflow/csharp/csni.cc View File

@@ -0,0 +1,7 @@
#include <iostream>
#include "tensorflow/c/c_api.h"

int main() {
printf("Hello from TensorFlow C library version %s", TF_Version());
return 0;
}

+ 19
- 0
src/tensorflow/csharp/eager/BUILD View File

@@ -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",

],
)

+ 4
- 0
src/tensorflow/csharp/eager/cswrap_tfe.h View File

@@ -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);

+ 23
- 0
src/tensorflow/csharp/eager/cswrap_tfe_src.cc View File

@@ -0,0 +1,23 @@
#include <cstring>
#include <thread>

#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<tensorflow::int64> input_ids = MakeTensorIDList(inputs);
}

}

Loading…
Cancel
Save