2626import com .google .protobuf .InvalidProtocolBufferException ;
2727import java .util .Collections ;
2828import java .util .IdentityHashMap ;
29+ import java .util .Locale ;
2930import java .util .Set ;
3031import java .util .stream .Collectors ;
3132import org .bytedeco .javacpp .PointerPointer ;
@@ -193,6 +194,9 @@ private static synchronized boolean hasGradient(String opType) {
193194 * <p>Note that this only works with graph gradients, and will eventually be deprecated in favor
194195 * of unified gradient support once it is fully supported by tensorflow core.
195196 *
197+ * <p><i>Warning: Custom gradient registration is currently not supported on Windows, see <a
198+ * href=https://github.com/tensorflow/java/issues/486>GitHub issue</a> for more info.</i>
199+ *
196200 * @param opType the type of op to register the gradient for. Should usually be an {@code OP_NAME}
197201 * field, i.e. {@link Add#OP_NAME}.
198202 * @param gradient the gradient function to use
@@ -201,6 +205,10 @@ private static synchronized boolean hasGradient(String opType) {
201205 */
202206 public static synchronized boolean registerCustomGradient (
203207 String opType , RawCustomGradient gradient ) {
208+ if (isWindowsOs ()) {
209+ throw new UnsupportedOperationException (
210+ "Custom gradient registration is not supported on Windows systems." );
211+ }
204212 if (hasGradient (opType )) {
205213 return false ;
206214 }
@@ -216,6 +224,9 @@ public static synchronized boolean registerCustomGradient(
216224 * generated op classes or custom op classes with the correct annotations. To operate on the
217225 * {@link org.tensorflow.GraphOperation} directly use {@link RawCustomGradient}.
218226 *
227+ * <p><i>Warning: Custom gradient registration is currently not supported on Windows, see <a
228+ * href=https://github.com/tensorflow/java/issues/486>GitHub issue</a> for more info.</i>
229+ *
219230 * @param inputClass the inputs class of op to register the gradient for.
220231 * @param gradient the gradient function to use
221232 * @return {@code true} if the gradient was registered, {@code false} if there was already a
@@ -225,8 +236,11 @@ public static synchronized boolean registerCustomGradient(
225236 */
226237 public static synchronized <T extends RawOpInputs <?>> boolean registerCustomGradient (
227238 Class <T > inputClass , CustomGradient <T > gradient ) {
239+ if (isWindowsOs ()) {
240+ throw new UnsupportedOperationException (
241+ "Custom gradient registration is not supported on Windows systems." );
242+ }
228243 OpInputsMetadata metadata = inputClass .getAnnotation (OpInputsMetadata .class );
229-
230244 if (metadata == null ) {
231245 throw new IllegalArgumentException (
232246 "Inputs Class "
@@ -253,4 +267,8 @@ public static synchronized <T extends RawOpInputs<?>> boolean registerCustomGrad
253267 gradientFuncs .add (g );
254268 return true ;
255269 }
270+
271+ private static boolean isWindowsOs () {
272+ return System .getProperty ("os.name" , "" ).toLowerCase (Locale .ENGLISH ).startsWith ("win" );
273+ }
256274}
0 commit comments