Skip to content
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

[Relax][Training] Optimizer API #107

Merged
merged 24 commits into from
Jan 29, 2023

Conversation

Ubospica
Copy link
Contributor

@Ubospica Ubospica commented Jan 20, 2023

This PR supports the new relax.training.optimizer.Optimizer API, which is the abstraction of optimizers in the relax training workflow. Specific optimizers should inherit it and override its methods.

See https://github.com/ACMClass-TVM-20/AD-Example/blob/main/optimizer_api_example.py for examples of its usage.

relax.optimizer is added as an alias for relax.training.optimizer.

The optimizer would be like:

class Optimizer:
    def __init__(self, params: Union[Var, List[Var]]) -> None:
        pass

    @property
    def state(self):
        """Return the state of the optimizer that would be updated in the optimization process."""
		pass

    def get_function(self) -> Function:
        """In any implementation of Optimizer, we will use blockbuilder to build a optimizer
        function that executes the update of parameters and the optimizer state.

        The optimizer function will take in a tuple of parameters, a tuple of gradients of
        parameters, and a tuple of optimizer states. It will return a tuple of updated parameters,
        and a tuple of optimizer states.
        """
        pass

Now we provide three common optimizers in module tvm.relax.training.optimizer:

  • SGD
  • SGD with momentum or Nesterov momentum
  • Adam

@Ubospica Ubospica changed the title [Relax] Optimizer API [WIP][Relax] Optimizer API Jan 20, 2023
@Ubospica Ubospica changed the title [WIP][Relax] Optimizer API [Relax] Optimizer API Jan 22, 2023
@Ubospica Ubospica changed the title [Relax] Optimizer API [Relax][Training] Optimizer API Jan 22, 2023
@Ubospica Ubospica force-pushed the mlc-dev/2023-01-19-optimizer branch 2 times, most recently from abf8b03 to ff37ee8 Compare January 22, 2023 18:13
python/tvm/relax/training/optimizer.py Outdated Show resolved Hide resolved
python/tvm/relax/training/optimizer.py Outdated Show resolved Hide resolved
python/tvm/relax/training/optimizer.py Outdated Show resolved Hide resolved

from typing import List, Union

import numpy as np
Copy link
Contributor Author

@Ubospica Ubospica Jan 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mypy reports error on this line:

error: Cannot find implementation or library stub for module named "numpy"

Now we uses # type: ignore to suppress the error.

@Ubospica Ubospica force-pushed the mlc-dev/2023-01-19-optimizer branch 2 times, most recently from ea1085c to 96bb1e4 Compare January 23, 2023 17:42
@Ubospica Ubospica force-pushed the mlc-dev/2023-01-19-optimizer branch 3 times, most recently from ce87cf6 to 56ccae2 Compare January 25, 2023 17:43
@SiriusNEO
Copy link
Contributor

Would it be great if we change the name of test files from test_optimizer to test_training_optimizer?

@MasterJH5574
Copy link
Member

See https://github.com/ACMClass-TVM-20/AD-Example/blob/main/optimizer_api_example.py for examples of its usage.

Seems I have no access to this repo.

python/tvm/relax/training/__init__.py Outdated Show resolved Hide resolved
python/tvm/relax/training/optimizer.py Outdated Show resolved Hide resolved
python/tvm/relax/training/optimizer.py Show resolved Hide resolved
python/tvm/relax/training/optimizer.py Outdated Show resolved Hide resolved
python/tvm/relax/training/optimizer.py Outdated Show resolved Hide resolved
tests/python/relax/test_training_optimizer.py Outdated Show resolved Hide resolved
tests/python/relax/test_training_optimizer.py Outdated Show resolved Hide resolved
python/tvm/relax/training/optimizer.py Outdated Show resolved Hide resolved
python/tvm/relax/training/optimizer.py Outdated Show resolved Hide resolved
@Ubospica
Copy link
Contributor Author

See ACMClass-TVM-20/AD-Example@main/optimizer_api_example.py for examples of its usage.

Seems I have no access to this repo.

@MasterJH5574 That repo has been set to public.

Copy link
Member

@Hzfengsy Hzfengsy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One general comment: should optimizer be a Python-only data structure or write it with C++?

python/tvm/relax/training/optimizer.py Outdated Show resolved Hide resolved
python/tvm/relax/training/optimizer.py Outdated Show resolved Hide resolved
python/tvm/relax/training/optimizer.py Outdated Show resolved Hide resolved
python/tvm/relax/training/optimizer.py Outdated Show resolved Hide resolved
python/tvm/relax/training/optimizer.py Outdated Show resolved Hide resolved
python/tvm/relax/training/optimizer.py Outdated Show resolved Hide resolved
python/tvm/relax/training/optimizer.py Outdated Show resolved Hide resolved
python/tvm/relax/training/optimizer.py Outdated Show resolved Hide resolved
python/tvm/relax/training/optimizer.py Outdated Show resolved Hide resolved
python/tvm/relax/training/optimizer.py Outdated Show resolved Hide resolved
@Ubospica
Copy link
Contributor Author

One general comment: should optimizer be a Python-only data structure or write it with C++?

The main difficulty of putting optimizer in C++ is that optimizers need numpy to construct optimizer states. Now we don't find good utils to construct tensors, like a zeros array with given shape, in C++.

@Hzfengsy
Copy link
Member

We have it in Relay and we can make a sperate one for Relax

Copy link
Member

@Hzfengsy Hzfengsy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Ubospica for the continuous improving

@Ubospica
Copy link
Contributor Author

Thank @SiriusNEO @MasterJH5574 @Hzfengsy @spectrometerHBH for the review!

@Ubospica Ubospica merged commit 77445d4 into mlc-ai:relax Jan 29, 2023
@Ubospica Ubospica deleted the mlc-dev/2023-01-19-optimizer branch January 29, 2023 08:14
MasterJH5574 pushed a commit that referenced this pull request Jan 31, 2023
MasterJH5574 pushed a commit that referenced this pull request Feb 8, 2023
* fix structural_equal_hash

(cherry picked from commit e7e962634999739a32129378f61cc95f58335447)

* address comment & pass the ci
MasterJH5574 pushed a commit that referenced this pull request Feb 8, 2023
spectrometerHBH pushed a commit to spectrometerHBH/relax that referenced this pull request Feb 9, 2023
* fix structural_equal_hash

(cherry picked from commit e7e962634999739a32129378f61cc95f58335447)

* address comment & pass the ci
MasterJH5574 pushed a commit that referenced this pull request Feb 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants