Skip to content

Commit fd789d4

Browse files
committed
add CSharp Native Interface in C++
1 parent ba499ab commit fd789d4

File tree

10 files changed

+133
-0
lines changed

10 files changed

+133
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,3 +335,8 @@ ASALocalRun/
335335
/tensorflowlib/linux/native/libtensorflow.so
336336
/src/TensorFlowNET.Core/tensorflow.dll
337337
/docs/build
338+
src/TensorFlowNET.Native/libtensorflow.dll
339+
src/TensorFlowNET.Native/bazel-*
340+
/src/TensorFlowNET.Native/libtensorflow.lib
341+
src/TensorFlowNET.Native/c_api.h
342+
/.vscode

src/TensorFlowNET.Native/BUILD

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Description:
2+
# CSharp Native Interface library intended for implementing the
3+
# TensorFlow .NET Standard API using the TensorFlow C library.
4+
5+
licenses(["notice"]) # Apache 2.0
6+
7+
cc_import(
8+
name = "libtensorflow",
9+
hdrs = ["c_api.h"],
10+
interface_library = "libtensorflow.lib",
11+
shared_library = "libtensorflow.dll",
12+
)
13+
14+
cc_binary(
15+
name = "csni",
16+
srcs = ["csni.cc"],
17+
deps = [":libtensorflow"],
18+
linkstatic = 0
19+
)

src/TensorFlowNET.Native/WORKSPACE

Whitespace-only changes.

src/TensorFlowNET.Native/csni.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <windows.h>
2+
#include <iostream>
3+
#include "c_api.h"
4+
typedef char* (__stdcall *TFFunc)();
5+
6+
int main() {
7+
HINSTANCE hinstLib = LoadLibrary(TEXT("libtensorflow.dll"));
8+
if (!hinstLib) {
9+
std::cout << "could not load the dynamic library" << std::endl;
10+
return EXIT_FAILURE;
11+
}
12+
13+
TFFunc version = (TFFunc) GetProcAddress(hinstLib, "TF_Version");
14+
if (!version) {
15+
std::cout << "could not locate the function" << std::endl;
16+
return EXIT_FAILURE;
17+
}
18+
19+
printf("Hello from TensorFlow C library version %s", version());
20+
return 0;
21+
}

src/tensorflow/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
### How to compile CSharp Native Interface
2+
3+
4+
git clone https://github.com/tensorflow/tensorflow
5+
6+
`cd tensorflow/tensorflow`
7+
8+
copy `csharp` folder to `tensorflow`, the csharp folder should be in the same parent directory with other language binding.
9+
10+
`cd csharp`
11+
12+
`bazel build //tensorflow/csharp:csni`

src/tensorflow/csharp/BUILD

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Description:
2+
# CSharp Native Interface library intended for implementing the
3+
# TensorFlow .NET Standard API using the TensorFlow C library.
4+
package(
5+
default_visibility = ["//visibility:private"],
6+
)
7+
8+
licenses(["notice"]) # Apache 2.0
9+
10+
load(
11+
"//tensorflow:tensorflow.bzl",
12+
"tf_cc_binary"
13+
)
14+
15+
16+
17+
tf_cc_binary(
18+
name = "csni",
19+
srcs = ["csni.cc"],
20+
deps = [
21+
"//tensorflow/c:c_api",
22+
"//tensorflow/csharp/eager:cswrap_tfe_lib"],
23+
)

src/tensorflow/csharp/csni.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <iostream>
2+
#include "tensorflow/c/c_api.h"
3+
4+
int main() {
5+
printf("Hello from TensorFlow C library version %s", TF_Version());
6+
return 0;
7+
}

src/tensorflow/csharp/eager/BUILD

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
load("//tensorflow:tensorflow.bzl", "tf_cc_binary")
2+
3+
cc_library(
4+
name = "cswrap_tfe_lib",
5+
srcs = [
6+
"cswrap_tfe_src.cc",
7+
],
8+
hdrs = [
9+
"cswrap_tfe.h",
10+
],
11+
visibility = [
12+
"//learning/deepmind/courier:__subpackages__",
13+
"//tensorflow:internal",
14+
],
15+
deps = [
16+
"//tensorflow/c:c_api",
17+
18+
],
19+
)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Record the gradient for a given op.
2+
extern void TFE_Py_RecordGradient(const char* op_name, void* inputs,
3+
void* attrs, void* results,
4+
const char* name);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <cstring>
2+
#include <thread>
3+
4+
#include "tensorflow/core/lib/core/errors.h"
5+
#include "cswrap_tfe.h"
6+
7+
#include "tensorflow/c/c_api.h"
8+
#include "tensorflow/c/c_api_internal.h"
9+
10+
#include "tensorflow/core/platform/protobuf.h"
11+
#include "tensorflow/core/platform/types.h"
12+
13+
14+
namespace {
15+
16+
void RecordGradient(const char* op_name, void* inputs,
17+
void* attrs, void* results,
18+
const char* name) {
19+
// std::vector<tensorflow::int64> input_ids = MakeTensorIDList(inputs);
20+
21+
}
22+
23+
}

0 commit comments

Comments
 (0)