Skip to content

Commit 99ea4f8

Browse files
committed
JavaCPP fixes, Span adapter
Signed-off-by: Ryan Nett <rnett@calpoly.edu>
1 parent 39e783b commit 99ea4f8

File tree

6 files changed

+118
-38
lines changed

6 files changed

+118
-38
lines changed

tensorflow-core/tensorflow-core-api/pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,19 @@
141141
</execution>
142142
</executions>
143143
</plugin>
144+
<plugin>
145+
<artifactId>maven-resources-plugin</artifactId>
146+
<version>3.1.0</version>
147+
<executions>
148+
<execution>
149+
<id>javacpp-parser</id>
150+
<phase>generate-sources</phase>
151+
<goals>
152+
<goal>resources</goal>
153+
</goals>
154+
</execution>
155+
</executions>
156+
</plugin>
144157
<plugin>
145158
<artifactId>maven-compiler-plugin</artifactId>
146159
<version>3.8.0</version>
@@ -210,6 +223,11 @@
210223
<includePaths>
211224
<includePath>${project.basedir}/</includePath>
212225
<includePath>${project.basedir}/bazel-${project.artifactId}/external/org_tensorflow/</includePath>
226+
<includePath>${project.basedir}/bazel-bin/external/org_tensorflow/</includePath>
227+
<includePath>${project.basedir}/bazel-${project.artifactId}/external/com_google_absl/</includePath>
228+
<includePath>${project.basedir}/bazel-${project.artifactId}/external/eigen_archive/</includePath>
229+
<includePath>${project.basedir}/bazel-${project.artifactId}/external/com_google_protobuf/src/</includePath>
230+
<includePath>${project.basedir}/target/classes/org/tensorflow/internal/c_api/include/</includePath>
213231
</includePaths>
214232
<linkPaths>
215233
<linkPath>${project.basedir}/bazel-bin/external/llvm_openmp/</linkPath>

tensorflow-core/tensorflow-core-api/src/gen/java/org/tensorflow/internal/c_api/TF_OperationDescription.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,6 @@ public class TF_OperationDescription extends Pointer {
1616
/** Pointer cast constructor. Invokes {@link Pointer#Pointer(Pointer)}. */
1717
public TF_OperationDescription(Pointer p) { super(p); }
1818

19-
public TF_OperationDescription(TF_Graph g, @Cast("const char*") BytePointer op_type,
20-
@Cast("const char*") BytePointer node_name) { super((Pointer)null); allocate(g, op_type, node_name); }
21-
private native void allocate(TF_Graph g, @Cast("const char*") BytePointer op_type,
22-
@Cast("const char*") BytePointer node_name);
23-
public TF_OperationDescription(TF_Graph g, String op_type,
24-
String node_name) { super((Pointer)null); allocate(g, op_type, node_name); }
25-
private native void allocate(TF_Graph g, String op_type,
26-
String node_name);
2719

2820
public native @ByRef NodeBuilder node_builder(); public native TF_OperationDescription node_builder(NodeBuilder setter);
2921
public native TF_Graph graph(); public native TF_OperationDescription graph(TF_Graph setter);

tensorflow-core/tensorflow-core-api/src/gen/java/org/tensorflow/internal/c_api/TF_Scope.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ public class TF_Scope extends Pointer {
9191
* {@code name/child_scope_name} as the prefix. The actual name will be unique
9292
* in the current scope. All other properties are inherited from the current
9393
* scope. If {@code child_scope_name} is empty, the {@code /} is elided. */
94-
public native @ByVal TF_Scope NewSubScope(BytePointer child_scope_name);
95-
public native @ByVal TF_Scope NewSubScope(String child_scope_name);
94+
public native @ByVal TF_Scope NewSubScope(@StdString BytePointer child_scope_name);
95+
public native @ByVal TF_Scope NewSubScope(@StdString String child_scope_name);
9696

9797
/** Return a new scope. All ops created within the returned scope will have
9898
* names of the form {@code name/StrCat(fragments...)[_suffix]} */
@@ -101,19 +101,19 @@ public class TF_Scope extends Pointer {
101101
* control dependencies the union of operations in the control_deps vector
102102
* and the control dependencies of the current scope. */
103103
public native @ByVal TF_Scope WithControlDependencies(
104-
@StdVector TF_Operation control_deps);
104+
@Cast("tensorflow::Operation*") @Span TF_Operation control_deps);
105105
/** Same as above, but convenient to add control dependency on the operation
106106
* producing the control_dep output. */
107-
public native @ByVal TF_Scope WithControlDependencies(@Const @ByRef TF_Output control_dep);
107+
public native @ByVal TF_Scope WithControlDependencies(@Cast("const tensorflow::Output*") @ByRef TF_Output control_dep);
108108

109109
/** Return a new scope. All ops created within the returned scope will have no
110110
* control dependencies on other operations. */
111111
public native @ByVal TF_Scope WithNoControlDependencies();
112112

113113
/** Return a new scope. All ops created within the returned scope will have
114114
* the device field set to 'device'. */
115-
public native @ByVal TF_Scope WithDevice(BytePointer device);
116-
public native @ByVal TF_Scope WithDevice(String device);
115+
public native @ByVal TF_Scope WithDevice(@StdString BytePointer device);
116+
public native @ByVal TF_Scope WithDevice(@StdString String device);
117117

118118
/** Returns a new scope. All ops created within the returned scope will have
119119
* their assigned device set to {@code assigned_device}. */
@@ -147,14 +147,14 @@ public class TF_Scope extends Pointer {
147147

148148
/** Return a unique name, using default_name if an op name has not been
149149
* specified. */
150-
public native BytePointer GetUniqueNameForOp(BytePointer default_name);
151-
public native String GetUniqueNameForOp(String default_name);
150+
public native @StdString BytePointer GetUniqueNameForOp(@StdString BytePointer default_name);
151+
public native @StdString String GetUniqueNameForOp(@StdString String default_name);
152152

153153
/** Update the status on this scope.
154154
* Note: The status object is shared between all children of this scope.
155155
* If the resulting status is not Status::OK() and exit_on_error_ is set on
156156
* this scope, this function exits by calling LOG(FATAL). */
157-
public native void UpdateStatus(@Const @ByRef TF_Status s);
157+
158158

159159
// START_SKIP_DOXYGEN
160160

@@ -172,7 +172,7 @@ public class TF_Scope extends Pointer {
172172
// TODO(skyewm): Graph is not part of public API
173173

174174

175-
public native @ByVal TF_Status status();
175+
176176

177177
/** If status() is Status::OK(), convert the Graph object stored in this scope
178178
* to a GraphDef proto and return Status::OK(). Otherwise, return the error

tensorflow-core/tensorflow-core-api/src/gen/java/org/tensorflow/internal/c_api/global/tensorflow.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3043,11 +3043,6 @@ public static native void TF_RegisterFilesystemPlugin(
30433043
// #endif // TENSORFLOW_C_C_API_H_
30443044

30453045

3046-
// Targeting ../TF_OperationDescription.java
3047-
3048-
3049-
3050-
30513046
// Parsed from tensorflow/c/kernels.h
30523047

30533048
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
@@ -3723,6 +3718,26 @@ public static native void TF_ShapeInferenceContextConcatenateShapes(
37233718
// #endif // TENSORFLOW_C_OPS_H_
37243719

37253720

3721+
// Parsed from tensorflow_adapters.h
3722+
3723+
/*
3724+
Copyright 2021 The TensorFlow Authors. All Rights Reserved.
3725+
Licensed under the Apache License, Version 2.0 (the "License");
3726+
you may not use this file except in compliance with the License.
3727+
You may obtain a copy of the License at
3728+
http://www.apache.org/licenses/LICENSE-2.0
3729+
Unless required by applicable law or agreed to in writing, software
3730+
distributed under the License is distributed on an "AS IS" BASIS,
3731+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
3732+
See the License for the specific language governing permissions and
3733+
limitations under the License.
3734+
=======================================================================
3735+
*/
3736+
3737+
// #include "absl/types/span.h"
3738+
3739+
3740+
37263741
// Parsed from tensorflow/c/eager/c_api.h
37273742

37283743
/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
@@ -4435,4 +4450,9 @@ public static native void TFE_ContextExportRunMetadata(TFE_Context ctx,
44354450
// #endif // TENSORFLOW_CC_FRAMEWORK_SCOPE_H_
44364451

44374452

4453+
// Targeting ../TF_OperationDescription.java
4454+
4455+
4456+
4457+
44384458
}

tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/internal/c_api/presets/tensorflow.java

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@
1717

1818
package org.tensorflow.internal.c_api.presets;
1919

20+
import java.lang.annotation.*;
2021
import java.util.List;
2122

2223
import org.bytedeco.javacpp.ClassProperties;
2324
import org.bytedeco.javacpp.LoadEnabled;
2425
import org.bytedeco.javacpp.Loader;
25-
import org.bytedeco.javacpp.annotation.NoException;
26-
import org.bytedeco.javacpp.annotation.Platform;
27-
import org.bytedeco.javacpp.annotation.Properties;
26+
import org.bytedeco.javacpp.annotation.*;
2827
import org.bytedeco.javacpp.tools.Info;
2928
import org.bytedeco.javacpp.tools.InfoMap;
3029
import org.bytedeco.javacpp.tools.InfoMapper;
@@ -48,15 +47,14 @@
4847
"tensorflow/c/tf_tensor.h",
4948
"tensorflow/c/tf_tstring.h",
5049
"tensorflow/c/c_api.h",
51-
"tensorflow/c/c_api_internal.h",
5250
// "tensorflow/c/env.h",
5351
"tensorflow/c/kernels.h",
5452
"tensorflow/c/ops.h",
53+
"tensorflow_adapters.h",
5554
"tensorflow/c/eager/c_api.h",
56-
"tensorflow/cc/framework/scope.h"
57-
},
58-
exclude = {
59-
"tensorflow/c/c_api_internal.h"
55+
"tensorflow/cc/framework/scope.h",
56+
// "tensorflow/cc/framework/ops.h",
57+
"tensorflow/c/c_api_internal.h",
6058
},
6159
link = "tensorflow_cc@.2",
6260
preload = {"iomp5", "mklml", "mklml_intel", "tensorflow_framework@.2"},
@@ -203,10 +201,10 @@ public void init(ClassProperties properties) {
203201

204202
public void map(InfoMap infoMap) {
205203
infoMap
204+
.put(new Info("TF_OperationDescription").pointerTypes("TF_OperationDescription").purify())
206205
.put(new Info("c_api_internal.h")
207206
.linePatterns("struct TF_OperationDescription \\{", "\\};"))
208207
.put(new Info("TF_CAPI_EXPORT", "TF_Bool").cppTypes().annotations())
209-
.put(new Info("tensorflow::string", "string").cppTypes("std::string"))
210208
.put(new Info("TF_Buffer::data").javaText("public native @Const Pointer data(); public native TF_Buffer data(Pointer data);"))
211209
.put(new Info("TF_Status").pointerTypes("TF_Status").base("org.tensorflow.internal.c_api.AbstractTF_Status"))
212210
.put(new Info("TF_Buffer").pointerTypes("TF_Buffer").base("org.tensorflow.internal.c_api.AbstractTF_Buffer"))
@@ -238,13 +236,15 @@ public void map(InfoMap infoMap) {
238236
.put(new Info("TFE_TensorHandle").pointerTypes("TFE_TensorHandle").base("org.tensorflow.internal.c_api.AbstractTFE_TensorHandle"))
239237
.put(new Info("TF_ShapeInferenceContextDimValueKnown", "TFE_NewTensorHandle(const tensorflow::Tensor&, TF_Status*)").skip())
240238
.put(new Info("tensorflow::Scope").javaNames("TF_Scope").pointerTypes("TF_Scope"))
241-
.put(new Info("TF_OperationDescription").pointerTypes("TF_OperationDescription"))
242239
.put(new Info("tensorflow::NodeBuilder").pointerTypes("NodeBuilder"))
243-
.put(new Info("tensorflow::Status").cppTypes("TF_Status"))
244-
.put(new Info("gtl::ArraySlice<TF_Operation>").cppTypes("std::vector<TF_Operation>"))
245-
.put(new Info("Output").cppTypes("TF_Output"))
246-
.put(new Info("Operation").cppTypes("TF_Operation"))
247-
.put(new Info("tensorflow::CompositeOpScopes", "tensorflow::Graph", "tensorflow::GraphDef", "tensorflow::Scope::graph",
240+
.put(new Info("tensorflow::string", "absl::string_view", "tensorflow::StringPiece").annotations("@StdString").valueTypes("BytePointer", "String").pointerTypes("BytePointer"))
241+
.put(new Info("absl::Span", "tensorflow::gtl::ArraySlice").annotations("@Span"))
242+
.put(new Info("tensorflow::Output").javaNames("TF_Output").cast())
243+
.put(new Info("tensorflow::Operation").javaNames("TF_Operation").cast())
244+
.put(new Info("tensorflow::CompositeOpScopes",
245+
"tensorflow::Graph",
246+
"tensorflow::GraphDef",
247+
"tensorflow::Scope::graph",
248248
"tensorflow::Scope::graph_as_shared_ptr",
249249
"tensorflow::Scope::ToGraphDef",
250250
"tensorflow::Scope::ToGraph",
@@ -257,8 +257,16 @@ public void map(InfoMap infoMap) {
257257
"tensorflow::Scope::ColocateWith",
258258
"tensorflow::Scope::WithXlaCluster",
259259
"tensorflow::Scope::WithAssignedDevice",
260+
"tensorflow::Scope::status",
261+
"tensorflow::Scope::UpdateStatus",
260262
"tensorflow::CreateOutputWithScope",
261263
"TF_OperationDescription::colocation_constraints"
262264
).skip());
263265
}
266+
267+
@Documented
268+
@Retention(RetentionPolicy.RUNTIME)
269+
@Target({ElementType.METHOD, ElementType.PARAMETER})
270+
@Cast({"absl::Span", "&"}) @Adapter("SpanAdapter")
271+
public @interface Span { String value() default ""; }
264272
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Copyright 2021 The TensorFlow Authors. All Rights Reserved.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
=======================================================================
13+
*/
14+
15+
#include "absl/types/span.h"
16+
17+
using namespace absl;
18+
19+
template<typename T> class SpanAdapter {
20+
public:
21+
SpanAdapter(T const * ptr, typename Span<T>::size_type size, void* owner) : ptr((T*)ptr), size(size), owner(owner),
22+
arr2(ptr ? Span<T>((T*)ptr, size) : Span<T>()), arr(arr2) { }
23+
SpanAdapter(const Span<T>& arr) : ptr(0), size(0), owner(0), arr2(arr), arr(arr2) { }
24+
SpanAdapter( Span<T>& arr) : ptr(0), size(0), owner(0), arr(arr) { }
25+
SpanAdapter(const Span<T>* arr) : ptr(0), size(0), owner(0), arr(*(Span<T>*)arr) { }
26+
void assign(T* ptr, typename Span<T>::size_type size, void* owner) {
27+
this->ptr = ptr;
28+
this->size = size;
29+
this->owner = owner;
30+
arr.set(ptr, size);
31+
}
32+
static void deallocate(void* owner) { free(owner); }
33+
operator T*() { size = arr.size(); return (T*)arr.data(); }
34+
operator Span<T>&() { return arr; }
35+
operator Span<T>*() { return ptr ? &arr : 0; }
36+
T* ptr;
37+
typename Span<T>::size_type size;
38+
void* owner;
39+
Span<T> arr2;
40+
Span<T>& arr;
41+
};
42+

0 commit comments

Comments
 (0)