-
Notifications
You must be signed in to change notification settings - Fork 223
Custom/gradients dispatch #632
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
bcf9e2e
7c7dc54
e2fa04b
1d43d4c
69a9a36
8d80312
1240293
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| /* Copyright 2026 The TensorFlow Authors. All Rights Reserved. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| ======================================================================= | ||
| */ | ||
| package org.tensorflow.op; | ||
|
|
||
| import java.lang.reflect.Constructor; | ||
| import java.util.List; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.ConcurrentMap; | ||
| import org.tensorflow.AbstractGradientAdapter; | ||
| import org.tensorflow.Graph; | ||
| import org.tensorflow.GraphOperation; | ||
| import org.tensorflow.Operand; | ||
| import org.tensorflow.Output; | ||
| import org.tensorflow.internal.c_api.TFJ_Scope; | ||
|
|
||
| final class DispatchingGradientAdapter extends AbstractGradientAdapter { | ||
|
|
||
| private final ConcurrentMap<String, RawCustomGradient> raw = new ConcurrentHashMap<>(); | ||
| private final ConcurrentMap<String, TypedEntry<?>> typed = new ConcurrentHashMap<>(); | ||
|
|
||
| private static String dupMsg(String opType, String existingKind, String newKind) { | ||
| return "A " | ||
| + existingKind | ||
| + " gradient is already registered for op type '" | ||
| + opType | ||
| + "'. Raw and typed registrations are mutually exclusive; cannot register " | ||
| + newKind | ||
| + "."; | ||
| } | ||
|
|
||
| static final class TypedEntry<T extends RawOpInputs<?>> { | ||
| final CustomGradient<T> grad; | ||
| final Class<T> inputClass; | ||
| final Constructor<T> ctor; | ||
|
|
||
| TypedEntry(CustomGradient<T> grad, Class<T> inputClass) { | ||
| this.grad = grad; | ||
| this.inputClass = inputClass; | ||
| try { | ||
| this.ctor = inputClass.getConstructor(org.tensorflow.GraphOperation.class); | ||
| } catch (NoSuchMethodException e) { | ||
| throw new IllegalArgumentException( | ||
| "Inputs class " + inputClass.getName() + " must have a public ctor(GraphOperation).", | ||
| e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void putRaw(String opType, RawCustomGradient g) { | ||
| if (typed.containsKey(opType)) { | ||
| throw new IllegalStateException(dupMsg(opType, "typed", "raw")); | ||
| } | ||
| RawCustomGradient prev = raw.putIfAbsent(opType, g); | ||
| if (prev != null) { | ||
| throw new IllegalStateException( | ||
| "A raw gradient is already registered for op type '" + opType + "'."); | ||
| } | ||
| } | ||
|
|
||
| <T extends RawOpInputs<?>> void putTyped( | ||
| String opType, CustomGradient<T> g, Class<T> inputClass) { | ||
| if (raw.containsKey(opType)) { | ||
| throw new IllegalStateException(dupMsg(opType, "raw", "typed")); | ||
| } | ||
| TypedEntry<?> prev = typed.putIfAbsent(opType, new TypedEntry<>(g, inputClass)); | ||
| if (prev != null) { | ||
| throw new IllegalStateException( | ||
| "A typed gradient is already registered for op type '" + opType + "'."); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| protected List<Operand<?>> apply( | ||
| Graph graph, TFJ_Scope scope, GraphOperation operation, List<Output<?>> gradInputs) { | ||
|
|
||
| final String opType = operation.type(); | ||
|
|
||
| RawCustomGradient rg = raw.get(opType); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logic prefers raw gradients over typed ones, but there isn't anything documented about why it prefers them or if it makes sense to add both raw and typed gradients for the same op. It would be good to clarify this, and if it doesn't make sense to have both kinds of gradients the adapter should reject them in the puts. |
||
| if (rg != null) { | ||
| // NativeScope & Ops constructors are package-private => must be in org.tensorflow.op | ||
| Scope nativeScope = | ||
| new NativeScope(scope, graph, operation.name()).withSubScope(operation.name()); | ||
| return rg.call(new Ops(nativeScope), operation, gradInputs); | ||
| } | ||
|
|
||
| @SuppressWarnings("rawtypes") | ||
| TypedEntry te = typed.get(opType); | ||
| if (te != null) { | ||
| return applyTyped(graph, scope, operation, gradInputs, te); | ||
| } | ||
|
|
||
| throw new IllegalStateException("No Java custom gradient registered for op type: " + opType); | ||
| } | ||
|
|
||
| private <T extends RawOpInputs<?>> List<Operand<?>> applyTyped( | ||
| Graph graph, | ||
| TFJ_Scope scope, | ||
| GraphOperation operation, | ||
| List<Output<?>> gradInputs, | ||
| TypedEntry<T> te) { | ||
| try { | ||
| T inputs = te.ctor.newInstance(operation); | ||
| Scope nativeScope = | ||
| new NativeScope(scope, graph, operation.name()).withSubScope(operation.name()); | ||
| return te.grad.call(new Ops(nativeScope), inputs, gradInputs); | ||
| } catch (ReflectiveOperationException e) { | ||
| throw new RuntimeException("Failed to instantiate inputs for " + te.inputClass.getName(), e); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /* Copyright 2026 The TensorFlow Authors. All Rights Reserved. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| ======================================================================= | ||
| */ | ||
| package org.tensorflow.op; | ||
|
|
||
| import org.tensorflow.internal.c_api.TFJ_GradFuncAdapter; | ||
|
|
||
| /** Public bridge to a single native gradient adapter. */ | ||
| public final class GradientDispatch { | ||
|
|
||
| // package-private adapter that can access NativeScope/Ops constructors | ||
| static final DispatchingGradientAdapter ADAPTER = new DispatchingGradientAdapter(); | ||
|
|
||
| private GradientDispatch() {} | ||
|
|
||
| public static TFJ_GradFuncAdapter adapter() { | ||
| return ADAPTER; | ||
| } | ||
|
|
||
| public static void putRaw(String opType, RawCustomGradient gradient) { | ||
| ADAPTER.putRaw(opType, gradient); | ||
| } | ||
|
|
||
| public static <T extends RawOpInputs<?>> void putTyped( | ||
| String opType, CustomGradient<T> gradient, Class<T> inputClass) { | ||
| ADAPTER.putTyped(opType, gradient, inputClass); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add the copyright and license header to the new files?