Skip to content

Commit

Permalink
Merge branch 'main' into cli
Browse files Browse the repository at this point in the history
  • Loading branch information
SkafteNicki authored Jun 28, 2024
2 parents a2cdec9 + 30545b3 commit 63ea5f9
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 10 deletions.
7 changes: 2 additions & 5 deletions s10_extra/exercise_files/onnx_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,16 @@ def wrapper(*args, **kwargs):
model = torchvision.models.resnet18()
model.eval()


dummy_input = torch.randn(1, 3, 224, 224)
if sys.platform == "win32":
# Windows doesn't support the new TorchDynamo-based ONNX Exporter
torch.onnx.export(
model,
torch.randn(1, 3, 224, 224),
dummy_input,
"resnet18.onnx",
input_names=["input.1"],
dynamic_axes={"input.1": {0: "batch_size", 2: "height", 3: "width"}},
)

compiled_model = torch.compile(model)

else:
torch.onnx.dynamo_export(model, dummy_input).save("resnet18.onnx")

Expand Down
20 changes: 20 additions & 0 deletions s10_extra/exercise_files/onnx_fastapi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import numpy as np
import onnxruntime
from fastapi import FastAPI

app = FastAPI()


@app.get("/predict")
def predict():
"""Predict using ONNX model."""
# Load the ONNX model
model = onnxruntime.InferenceSession("model.onnx")

# Prepare the input data
input_data = {"input": np.random.rand(1, 3).astype(np.float32)}

# Run the model
output = model.run(None, input_data)

return {"output": output[0].tolist()}
8 changes: 8 additions & 0 deletions s10_extra/ml_deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,14 @@ an developer friendly framework, however it has historically been slow to run in
8. (Optional) Assuming you have completed the module on [FastAPI](../s7_deployment/apis.md) try creating a small
FastAPI application that serves a model using the ONNX runtime.
??? success "Solution"
Here is a simple example of how to create a FastAPI application that serves a model using the ONNX runtime.
```python linenums="1" title="onnx_fastapi.py"
--8<-- "s10_extra/exercise_files/onnx_fastapi.py"
```
This completes the exercises on the ONNX format. Do note that one limitation of the ONNX format is that is is based on
[ProtoBuf](https://protobuf.dev/), which is a binary format. A protobuf file can have a maximum size of 2GB, which means
that the `.onnx` format is not enough for very large models. However, through the use of
Expand Down
37 changes: 37 additions & 0 deletions s1_development_environment/package_manager.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,24 +185,61 @@ in the exercise folder.
because the latest version of Python is often not supported by all dependencies. You can always check the status
of different Python version support [here](https://devguide.python.org/versions/).

??? success "Solution"

```bash
conda create --name my_environment python=3.11
```

4. Which `conda` command gives you a list of all the environments that you have created?

??? success "Solution"

```bash
conda env list
```

5. Which `conda` command gives you a list of the packages installed in the current environment?

??? success "Solution"

```bash
conda list
```

1. How do you easily export this list to a text file? Do this, and make sure you export it to
a file called `environment.yaml`, as conda uses another format by default than `pip`.

??? success "Solution"

```bash
conda list --explicit > environment.yaml
```

2. Inspect the file to see what is in it.

3. The `environment.yaml` file you have created is one way to secure *reproducibility* between users because
anyone should be able to get an exact copy of your environment if they have your `environment.yaml` file.
Try creating a new environment directly from your `environment.yaml` file and check that the packages being
installed exactly match what you originally had.

??? success "Solution"

```bash
conda env create --file environment.yaml
```

6. As the introduction states, it is fairly safe to use `pip` inside `conda` today. What is the corresponding `pip`
command that gives you a list of all `pip` installed packages? And how do you export this to `requirements.txt`
file?

??? success "Solution"

```bash
pip list # List all installed packages
pip freeze > requirements.txt # Export all installed packages to a requirements.txt file
```

7. If you look through the requirements that both `pip` and `conda` produce then you will see that it
is often filled with a lot more packages than what you are using in your project. What you are interested in are the
packages that you import in your code: `from package import module`. One way to get around this is to use the
Expand Down
29 changes: 24 additions & 5 deletions s2_organisation_and_version_control/git.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,16 @@ working together on the same project.

2. Move/copy the three files from yesterday into the repository (and any other that you made)

3. Add the files to a commit by using `git add` command (1)
{ .annotate }
3. Add the files to a commit by using `git add` command

4. Commit the files using `git commit` command where you use the `-m` argument to provide a commit message (1).
{ .annotate }

1. :man_raising_hand: Writing good commit message is a skill in itself. A commit message should be short but
informative about the work you are trying to commit. Try to practise writing good commit messages
throughout the course. You can see
[this guideline](https://github.com/joelparkerhenderson/git-commit-message) for help.

4. Commit the files using `git commit`

5. Finally push the files to your repository using `git push`. Make sure to check online that the files have been
updated in your repository.

Expand All @@ -139,10 +139,15 @@ working together on the same project.
git checkout -b <my_branch_name>
```

Afterwards, you can use `git checkout` to change between branches (remember to commit your work!)
Afterwards, you can use `git checkout` (1) to change between branches (remember to commit your work!)
Try adding something (a file, a new line of code etc.) to the newly created branch, commit it and
try changing back to master afterwards. You should hopefully see whatever you added on the branch
is not present on the main branch.
{ .annotate}

1. :man_raising_hand: The `git checkout` command is used for a lot of different things in git. It can be used to
change branches, to revert changes and to create new branches. An alternative is using `git switch` and
`git restore` which are more modern commands.

3. If you do not already have a cloned version of this repository belonging to the course, make sure to make one!
I am continuously updating/changing some of the material during the course and I therefore recommend that you
Expand Down Expand Up @@ -182,10 +187,24 @@ working together on the same project.
[page](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/configuring-a-remote-repository-for-a-fork)
, and set a remote upstream for the repository you just forked.

??? success "Solution"

```bash
git remote add upstream <url-to-original-repo>
```

6. After setting the upstream branch, we need to pull and merge any update. Take a look on this
[page](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork)
and figure out how to do this.

??? success "Solution"

```bash
git fetch upstream
git checkout main
git merge upstream/main
```

7. As a final exercise we want to simulate a *merge conflict*, which happens when two users try to commit changes
to exactly same lines of code in the codebase, and git is not able to resolve how the different commits should be
integrated.
Expand Down

0 comments on commit 63ea5f9

Please sign in to comment.