Framework-agnostic rework#35
Open
Agustin-Picard wants to merge 227 commits into
Open
Conversation
…match torch + adjust tests
… linear layer, etc)
…se backend utilization to reduce framework branching
…odel surgery methods
Member
Author
|
Thanks for the review. I went through the requested changes and pushed a follow-up series addressing the main points.
At this point, I think the review concerns around PyTorch version support, backend test coverage, backend-specific logic placement, and typing are all addressed much more directly than in the previous revision. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Going framework-agnostic!
This PR includes a complete rework of Influenciae to make it natively compatible with both TF and PyTorch.
Due to the distance to the latest update, it also includes a rework of the CI/CD and dependencies.
The backend
To actually get the library to work on two different tensor frameworks at the same time, we implemented a
BaseBackendobject that collects all the different functionality from these frameworks that we will need for the whole library. Then, we produce the actual implementations in TensorFlow and PyTorch inTensorFlowBackendandPyTorchBackendrespectively. Essentially, they implement basic functions like executingreduce_sum,zerosorgradientsin their respective frameworks.In practice, we use this to replace the tensor operations that were previously done in TF using this "agnostic" backend object. The correct backend is instantiated upon detection of the framework that is being used by inspecting the users' tensors/models.
The backends' operations are thoroughly tested, as is the parity between the two.
The influence calculators
Apart from specific cases (ie,
RPS_L2), most of the existing implementations of the influence calculators haven't been modified. Now, all tensor operations pass through the backend that is automatically detected. This means that the TensorFlow unit tests are mostly the same as before, ensuring continuity in the TensorFlow implementation.New tests are introduced for the PyTorch implementations, while the existing TensorFlow unit tests remain largely unchanged.
The utils
Once the backend had been taken care of, we needed to re-implement the utility functions that were more framework-specific: the backtracking line search optimizer, the batch sort dictionary, the conjugate gradient descent and lissa solvers, and the linear nearest neighbors.
As with the influence calculators, new independent tests are added for the PyTorch implementations.
Lazy datasets and operations
One of the main features of this library was the fact that implementations were lazy, allowing for large amounts of data to be computed in batches and in a streamed fashion. This worked well because
tf.data.Datasetimplemented these features, and we made sure to remain in that context.However, basic PyTorch doesn't provide a similar solution by default. Hence, we introduce the
LazyDatasetstructure incommonto provide this feature in both frameworks. It also provides wrappers for themap,batch,cache,unbatch,zip,shuffle, andtakeoperations we use on the TensorFlow side of the library. The simplest way to access it is viato_lazy_datasetincommon.We also create the
DatasetLiketype to check for this type in the relevant functions.Imports
Being backend agnostic also requires being careful when to import the different frameworks so there is never a leaked import of the other framework. In this case, we opted for lazy imports and a module for optional imports in
common.__init__.pyandcommon._optional_imports.pyrespectively.Benchmarks
The basic objects in the benchmark module have been re-implemented so that they work in PyTorch, but the CIFAR10 benchmark remains TF-specific. If users demand a PyTorch version, we will implement it.
CI/CD
The latest release of this library dates back to 2023. As such, most of the dependencies have evolved, and a new compatibility matrix had to be developed in this PR. We aim to support Python versions 3.11, 3.12, and 3.13; TensorFlow versions 2.12 through 2.20; and Torch versions 1.13 through 2.10.
Pylint is run for the different Python versions. MyPy is run only once.
The unit tests are run for the min and max versions of Torch and TensorFlow for each version of Python to guarantee compatibility in the extremes. There are also runs for both backends installed on the different Python versions to ensure that everything works well throughout the supported versions.