Skip to content

Commit c098372

Browse files
committed
Working prototype
Signed-off-by: Ryan Nett <[email protected]>
1 parent 8f8aadc commit c098372

File tree

8 files changed

+116
-9
lines changed

8 files changed

+116
-9
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@
4747
<artifactId>ndarray</artifactId>
4848
<version>${ndarray.version}</version>
4949
</dependency>
50+
<dependency>
51+
<groupId>org.slf4j</groupId>
52+
<artifactId>slf4j-api</artifactId>
53+
<version>1.7.31</version>
54+
</dependency>
5055
<dependency>
5156
<groupId>org.junit.jupiter</groupId>
5257
<artifactId>junit-jupiter-api</artifactId>
@@ -73,6 +78,12 @@
7378
<artifactId>jmh-generator-annprocess</artifactId>
7479
<scope>test</scope>
7580
</dependency>
81+
<dependency>
82+
<groupId>org.slf4j</groupId>
83+
<artifactId>slf4j-jdk14</artifactId>
84+
<version>1.7.31</version>
85+
<scope>test</scope>
86+
</dependency>
7687
</dependencies>
7788

7889
<profiles>

tensorflow-core/tensorflow-core-api/src/gen/annotations/org/tensorflow/op/Ops.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,10 +376,10 @@ public final class Ops {
376376

377377
public final SignalOps signal;
378378

379-
public final QuantizationOps quantization;
380-
381379
public final TrainOps train;
382380

381+
public final QuantizationOps quantization;
382+
383383
private final Scope scope;
384384

385385
private Ops(Scope scope) {
@@ -402,8 +402,8 @@ private Ops(Scope scope) {
402402
math = new MathOps(this);
403403
audio = new AudioOps(this);
404404
signal = new SignalOps(this);
405-
quantization = new QuantizationOps(this);
406405
train = new TrainOps(this);
406+
quantization = new QuantizationOps(this);
407407
}
408408

409409
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ public class TFLogEntry extends Pointer {
2626
public native @StdString @Cast({"char*", "std::string&&"}) BytePointer FName();
2727
public native int Line();
2828
public native @StdString @Cast({"char*", "std::string&&"}) BytePointer ToString();
29-
public native @StdString @Cast({"", "", "std::string"}) BytePointer text_message();
29+
3030
}

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,20 @@
1212
@Namespace("tensorflow") @Properties(inherit = org.tensorflow.internal.c_api.presets.tensorflow.class)
1313
public class TFLogSink extends Pointer {
1414
static { Loader.load(); }
15+
/** Default native constructor. */
16+
public TFLogSink() { super((Pointer)null); allocate(); }
17+
/** Native array allocator. Access with {@link Pointer#position(long)}. */
18+
public TFLogSink(long size) { super((Pointer)null); allocateArray(size); }
1519
/** Pointer cast constructor. Invokes {@link Pointer#Pointer(Pointer)}. */
1620
public TFLogSink(Pointer p) { super(p); }
21+
private native void allocate();
22+
private native void allocateArray(long size);
23+
@Override public TFLogSink position(long position) {
24+
return (TFLogSink)super.position(position);
25+
}
26+
@Override public TFLogSink getPointer(long i) {
27+
return new TFLogSink(this).position(position + i);
28+
}
1729

1830

1931
// `Send` is called synchronously during the log statement. The logging
@@ -24,7 +36,7 @@ public class TFLogSink extends Pointer {
2436
// `e` is guaranteed to remain valid until the subsequent call to
2537
// `WaitTillSent` completes, so implementations may store a pointer to or
2638
// copy of `e` (e.g. in a thread local variable) for use in `WaitTillSent`.
27-
public native void Send(@Const @ByRef TFLogEntry entry);
39+
@Virtual(true) public native void Send(@Const @ByRef TFLogEntry entry);
2840

2941
// `WaitTillSent` blocks the calling thread (the thread that generated a log
3042
// message) until the sink has finished processing the log message.
@@ -34,5 +46,5 @@ public class TFLogSink extends Pointer {
3446
// The default implementation returns immediately. Like `Send`,
3547
// implementations should be careful not to call `LOG` or `CHECK or take any
3648
// locks that might be held by the `LOG` caller, to avoid deadlock.
37-
public native void WaitTillSent();
49+
@Virtual public native void WaitTillSent();
3850
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Copyright 2021 The TensorFlow Authors. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
=======================================================================
16+
17+
*/
18+
package org.tensorflow;
19+
20+
import org.bytedeco.javacpp.Pointer;
21+
import org.slf4j.ILoggerFactory;
22+
import org.slf4j.Logger;
23+
import org.slf4j.LoggerFactory;
24+
import org.tensorflow.internal.c_api.TFLogEntry;
25+
import org.tensorflow.internal.c_api.TFLogSink;
26+
import org.tensorflow.internal.c_api.global.tensorflow;
27+
28+
public class NativeLogSink extends TFLogSink {
29+
private static final ILoggerFactory factory = LoggerFactory.getILoggerFactory();
30+
private static final Logger logger = LoggerFactory.getLogger(NativeLogSink.class);
31+
NativeLogSink() {
32+
super();
33+
}
34+
35+
@Override
36+
public void Send(TFLogEntry entry) {
37+
//TODO make work, blocked by https://github.com/tensorflow/tensorflow/issues/44995#issuecomment-869091090
38+
System.out.printf(
39+
"Log message: Severity: %d, Fname: %s, line: %s, string: %s\n", entry.log_severity(), entry.FName().getString(), entry.Line(), entry.ToString().getString());
40+
// Logger logger = factory.getLogger(entry.FName().getString());
41+
// switch (entry.log_severity()){
42+
// case tensorflow.kWarning:
43+
// logger.warn(entry.ToString().getString());
44+
// break;
45+
// case tensorflow.kError:
46+
// case tensorflow.kFatal:
47+
// logger.error(entry.ToString().getString());
48+
// break;
49+
// default:
50+
// logger.info(entry.ToString().getString());
51+
// }
52+
}
53+
54+
@Override
55+
public void WaitTillSent() {
56+
}
57+
}

tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/TensorFlow.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.tensorflow;
1717

18+
import static org.tensorflow.internal.c_api.global.tensorflow.TFAddLogSink;
1819
import static org.tensorflow.internal.c_api.global.tensorflow.TF_DeleteBuffer;
1920
import static org.tensorflow.internal.c_api.global.tensorflow.TF_DeleteLibraryHandle;
2021
import static org.tensorflow.internal.c_api.global.tensorflow.TF_GetAllOpList;
@@ -96,6 +97,13 @@ public static OpList loadLibrary(String filename) {
9697
}
9798
}
9899

100+
@SuppressWarnings("FieldCanBeLocal")
101+
private static NativeLogSink sink;
102+
private static void setupLogger(){
103+
sink = new NativeLogSink();
104+
TFAddLogSink(sink);
105+
}
106+
99107
private static TF_Library libraryLoad(String filename) {
100108
try (PointerScope scope = new PointerScope()) {
101109
TF_Status status = TF_Status.newStatus();
@@ -137,5 +145,6 @@ private TensorFlow() {}
137145
e.printStackTrace();
138146
throw e;
139147
}
148+
setupLogger();
140149
}
141150
}

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -403,15 +403,27 @@ public void map(InfoMap infoMap) {
403403
.cast()
404404
.valueTypes("long")
405405
.pointerTypes("LongPointer", "long[]"))
406-
.put(new Info("string", "std::string", "tensorflow::string").annotations("@StdString")
407-
.valueTypes("@Cast({\"char*\", \"std::string&&\"}) BytePointer", "@Cast({\"char*\", \"std::string&&\"}) String")
408-
.pointerTypes("@Cast({\"char*\", \"std::string*\"}) BytePointer"))
406+
.put(
407+
new Info("string", "std::string", "tensorflow::string")
408+
.annotations("@StdString")
409+
.valueTypes(
410+
"@Cast({\"char*\", \"std::string&&\"}) BytePointer",
411+
"@Cast({\"char*\", \"std::string&&\"}) String")
412+
.pointerTypes("@Cast({\"char*\", \"std::string*\"}) BytePointer"))
413+
// .put(
414+
// new Info("absl::string_view")
415+
// .annotations("@StdString")
416+
// .valueTypes(
417+
// "@Cast({\"char*\", \"std::string&&\", \"std::string\"}) BytePointer",
418+
// "@Cast({\"char*\", \"std::string&&\", \"std::string\"}) String"))
409419
.put(
410420
new Info("absl::LogSeverity", "LogSeverity", "tensorflow::LogSeverity")
411421
.cast()
412422
.valueTypes("int")
413423
.pointerTypes("IntPointer", "int[]"))
414424
.put(new Info("tensorflow::TFLogEntry").purify())
425+
.put(new Info("tensorflow::TFLogSink").virtualize())
426+
.put(new Info("tensorflow::TFLogEntry::text_message").skip())
415427
.put(
416428
new Info(
417429
"tensorflow::internal::LogEveryNSecState",

tensorflow-framework/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@
6464
<artifactId>jmh-generator-annprocess</artifactId>
6565
<scope>test</scope>
6666
</dependency>
67+
<dependency>
68+
<groupId>org.slf4j</groupId>
69+
<artifactId>slf4j-jdk14</artifactId>
70+
<version>1.7.31</version>
71+
<scope>test</scope>
72+
</dependency>
6773
<!-- Include native binaries dependencies only for testing -->
6874
<dependency>
6975
<groupId>org.tensorflow</groupId>

0 commit comments

Comments
 (0)