|
| 1 | +/* |
| 2 | + * Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance |
| 5 | + * with the License. A copy of the License is located at |
| 6 | + * |
| 7 | + * http://aws.amazon.com/apache2.0/ |
| 8 | + * |
| 9 | + * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES |
| 10 | + * OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions |
| 11 | + * and limitations under the License. |
| 12 | + */ |
| 13 | +package ai.djl.patch; |
| 14 | + |
| 15 | +import ai.djl.Model; |
| 16 | +import ai.djl.ndarray.NDArray; |
| 17 | +import ai.djl.nn.Block; |
| 18 | +import ai.djl.nn.Parameter; |
| 19 | +import ai.djl.nn.ParameterList; |
| 20 | +import ai.djl.training.GradientCollector; |
| 21 | +import ai.djl.util.Pair; |
| 22 | + |
| 23 | +import java.util.Map; |
| 24 | +import java.util.concurrent.ConcurrentHashMap; |
| 25 | + |
| 26 | +/** The basic implementation of a {@link ParamPatch}. */ |
| 27 | +public class BasicParamPatch extends ParamPatch { |
| 28 | + |
| 29 | + Map<String, NDArray> data; |
| 30 | + |
| 31 | + /** |
| 32 | + * Constructs a {@link BasicParamPatch} with patching data. |
| 33 | + * |
| 34 | + * @param data the patching data |
| 35 | + */ |
| 36 | + public BasicParamPatch(Map<String, NDArray> data) { |
| 37 | + this.data = data; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Makes a patch by comparing two models. |
| 42 | + * |
| 43 | + * @param source the source model |
| 44 | + * @param target the target model |
| 45 | + * @return a patch that would transform the source model to the target model |
| 46 | + */ |
| 47 | + public static BasicParamPatch makePatch(Model source, Model target) { |
| 48 | + return BasicParamPatch.makePatch(source.getBlock(), target.getBlock()); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Makes a patch by comparing two blocks. |
| 53 | + * |
| 54 | + * @param source the source block |
| 55 | + * @param target the target block |
| 56 | + * @return a patch that would transform the source block to the target block |
| 57 | + */ |
| 58 | + public static BasicParamPatch makePatch(Block source, Block target) { |
| 59 | + return BasicParamPatch.makePatch(source.getParameters(), target.getParameters()); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Makes a patch by comparing two {@link ParameterList}s. |
| 64 | + * |
| 65 | + * @param source the source {@link ParameterList} |
| 66 | + * @param target the target {@link ParameterList} |
| 67 | + * @return a patch that would transform the source {@link ParameterList} to the target {@link |
| 68 | + * ParameterList}. |
| 69 | + */ |
| 70 | + public static BasicParamPatch makePatch(ParameterList source, ParameterList target) { |
| 71 | + Map<String, NDArray> data = new ConcurrentHashMap<>(source.size()); |
| 72 | + for (Pair<String, Parameter> sourcePair : source) { |
| 73 | + String key = sourcePair.getKey(); |
| 74 | + NDArray patchValue = target.get(key).getArray().sub(sourcePair.getValue().getArray()); |
| 75 | + data.put(key, patchValue); |
| 76 | + } |
| 77 | + return new BasicParamPatch(data); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Makes a patch from gradients. |
| 82 | + * |
| 83 | + * <p>This does not include learning rates or any other data from the {@link |
| 84 | + * ai.djl.training.optimizer.Optimizer}. |
| 85 | + * |
| 86 | + * <p>Making the patch does not modify the existing gradients. After this, you can call {@link |
| 87 | + * GradientCollector#zeroGradients()} to clear the gradients. |
| 88 | + * |
| 89 | + * @param block the block for which to collect gradients |
| 90 | + * @param gradientCollector the {@link GradientCollector} of the gradients |
| 91 | + * @return the gradients as a {@link BasicParamPatch}. |
| 92 | + */ |
| 93 | + public static BasicParamPatch makePatch(Block block, GradientCollector gradientCollector) { |
| 94 | + ParameterList params = block.getParameters(); |
| 95 | + Map<String, NDArray> data = new ConcurrentHashMap<>(params.size()); |
| 96 | + for (Pair<String, Parameter> param : params) { |
| 97 | + String key = param.getKey(); |
| 98 | + // Get gradient * -1 to account for gradient being subtracted from param |
| 99 | + NDArray patchValue = param.getValue().getArray().getGradient().duplicate().mul(-1); |
| 100 | + data.put(key, patchValue); |
| 101 | + } |
| 102 | + return new BasicParamPatch(data); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Makes a patch from gradients. |
| 107 | + * |
| 108 | + * <p>This does not include learning rates or any other data from the {@link |
| 109 | + * ai.djl.training.optimizer.Optimizer}. |
| 110 | + * |
| 111 | + * <p>Making the patch does not modify the existing gradients. After this, you can call {@link |
| 112 | + * GradientCollector#zeroGradients()} to clear the gradients. |
| 113 | + * |
| 114 | + * @param model the model for which to collect gradients |
| 115 | + * @param gradientCollector the {@link GradientCollector} of the gradients |
| 116 | + * @return the gradients as a {@link BasicParamPatch}. |
| 117 | + */ |
| 118 | + public static BasicParamPatch makePatch(Model model, GradientCollector gradientCollector) { |
| 119 | + return makePatch(model.getBlock(), gradientCollector); |
| 120 | + } |
| 121 | + |
| 122 | + /** {@inheritDoc} */ |
| 123 | + @Override |
| 124 | + public NDArray getPatch(String paramName) { |
| 125 | + return data.get(paramName).duplicate(); |
| 126 | + } |
| 127 | + |
| 128 | + /** {@inheritDoc} */ |
| 129 | + @Override |
| 130 | + public void close() { |
| 131 | + for (NDArray d : data.values()) { |
| 132 | + d.close(); |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments