Skip to content

Commit a5ae4c4

Browse files
committed
Update code and text
1 parent 5c7ba46 commit a5ae4c4

File tree

3 files changed

+123
-133
lines changed

3 files changed

+123
-133
lines changed

articles/machine-learning/how-to-inference-server-http.md

Lines changed: 97 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Debug with Azure Machine Learning inference server
2+
title: Debug with the Azure Machine Learning inference server
33
titleSuffix: Azure Machine Learning
44
description: Learn how to enable local development with Azure Machine Learning inference HTTP server, and debug scoring scripts or endpoints before you deploy to the cloud.
55
author: msakande
@@ -15,33 +15,27 @@ ms.date: 08/21/2024
1515
#customer intent: As a developer, I want to work with the Azure Machine Learning inference HTTP server so I can debug scoring scripts or endpoints before deployment.
1616
---
1717

18-
# Debug scoring scripts with Azure Machine Learning inference HTTP server
18+
# Debug scoring scripts by using the Azure Machine Learning inference HTTP server
1919

20-
The Azure Machine Learning inference HTTP server is a Python package that exposes your scoring function as an HTTP endpoint and wraps the Flask server code and dependencies into a singular package. The server is included in the [prebuilt Docker images for inference](concept-prebuilt-docker-images-inference.md) that are used when deploying a model with Azure Machine Learning. Using the package alone, you can deploy the model locally for production, and easily validate your scoring (entry) script in a local development environment. If there's a problem with the scoring script, the server returns an error and the location of the error.
20+
The Azure Machine Learning inference HTTP server is a Python package that exposes your scoring function as an HTTP endpoint and wraps the Flask server code and dependencies into a singular package. The server is included in the [prebuilt Docker images for inference](concept-prebuilt-docker-images-inference.md) that are used when you deploy a model in Azure Machine Learning. When you use the package alone, you can deploy the model locally for production, and easily validate your scoring (entry) script in a local development environment. If there's a problem with the scoring script, the server returns an error and the location of the error.
2121

22-
The server can also be used to create validation gates in a continuous integration and deployment pipeline. For example, you can start the server with the candidate script and run the test suite against the local endpoint.
22+
You can also use the server to create validation gates in a continuous integration and deployment pipeline. For example, you can start the server with the candidate script and run the test suite against the local endpoint.
2323

24-
This article supports developers who want to use the inference server to debug locally and describes how to use the inference server with online endpoints on Windows.
24+
This article supports developers who want to use the inference server to debug locally. The Azure Machine Learning inference HTTP server runs on Windows and Linux based operating systems.
2525

2626
## Prerequisites
2727

28-
To use the Azure Machine Learning inference HTTP server for local debugging, your configuration must include the following components:
29-
3028
- Python 3.8 or later
3129
- Anaconda
3230

33-
The Azure Machine Learning inference HTTP server runs on Windows and Linux based operating systems.
34-
3531
## Explore local debugging options for online endpoints
3632

3733
By debugging endpoints locally before you deploy to the cloud, you can catch errors in your code and configuration earlier. To debug endpoints locally, you have several options, including:
3834

39-
- The Azure Machine Learning inference HTTP server
40-
- A [local endpoint](how-to-debug-managed-online-endpoints-visual-studio-code.md)
35+
- The Azure Machine Learning inference HTTP server.
36+
- A [local endpoint](how-to-debug-managed-online-endpoints-visual-studio-code.md).
4137

42-
This article describes how to work with the Azure Machine Learning inference HTTP server on Windows.
43-
44-
The following table provides an overview of scenarios to help you choose the best option:
38+
The following table provides an overview of the support in each option for various debugging scenarios:
4539

4640
| Scenario | Inference HTTP server | Local endpoint |
4741
| --- | :---: | :---: |
@@ -50,38 +44,30 @@ The following table provides an overview of scenarios to help you choose the bes
5044
| Update deployment configurations (deployment, environment, code, model) | No | Yes |
5145
| Integrate Microsoft Visual Studio Code (VS Code) Debugger | Yes | Yes |
5246

53-
When you run the inference HTTP server locally, you can focus on debugging your scoring script without concern for deployment container configurations.
54-
55-
## Install azureml-inference-server-http package
56-
57-
To install the `azureml-inference-server-http` package, run the following command:
58-
59-
```bash
60-
python -m pip install azureml-inference-server-http
61-
```
47+
This article describes how to use the Azure Machine Learning inference HTTP server with online endpoints on Windows.
6248

63-
> [!NOTE]
64-
> To avoid package conflicts, install the inference HTTP server in a virtual environment.
65-
> You can use the `pip install virtualenv` command to enable virtual environments for your configuration.
49+
When you run the inference HTTP server locally, you can focus on debugging your scoring script without concern for deployment container configurations.
6650

6751
## Debug your scoring script locally
6852

6953
To debug your scoring script locally, you have several options for testing the server behavior:
7054

71-
- Try a dummy scoring script.
55+
- Use a dummy scoring script.
7256
- Use Visual Studio Code to debug with the [azureml-inference-server-http](https://pypi.org/project/azureml-inference-server-http/) package.
73-
- Run an actual scoring script, model file, and environment file from our [examples repo](https://github.com/Azure/azureml-examples).
57+
- Run an actual scoring script, model file, and environment file from the [examples repo](https://github.com/Azure/azureml-examples).
58+
59+
The following sections provide information about each option.
7460

75-
### Test server behavior with dummy scoring script
61+
### Use a dummy scoring script to test server behavior
7662

77-
1. Create a directory named _server\_quickstart_ to hold your files:
63+
1. Create a directory named server\_quickstart to hold your files:
7864

7965
```bash
8066
mkdir server_quickstart
8167
cd server_quickstart
8268
```
8369

84-
1. To avoid package conflicts, create a virtual environment, such as _myenv_, and activate it:
70+
1. To avoid package conflicts, create a virtual environment, such as `myenv`, and activate it:
8571

8672
```bash
8773
python -m virtualenv myenv
@@ -90,9 +76,6 @@ To debug your scoring script locally, you have several options for testing the s
9076
> [!NOTE]
9177
> On Linux, run the `source myenv/bin/activate` command to activate the virtual environment.
9278
93-
<!-- Reviewer: The 'source' command appears to apply to Linux only.
94-
I found that the 'python -m virtualenv...' command (as opposed to 'python -m venv ...') both creates and activates the env. -->
95-
9679
After you test the server, you can run the `deactivate` command to deactivate the Python virtual environment.
9780

9881
1. Install the `azureml-inference-server-http` package from the [pypi](https://pypi.org/project/azureml-inference-server-http/) feed:
@@ -101,16 +84,13 @@ To debug your scoring script locally, you have several options for testing the s
10184
python -m pip install azureml-inference-server-http
10285
```
10386

104-
1. Create your entry script. The following example creates a basic entry script and saves it to a file named _score.py_:
87+
1. Create your entry script. The following example creates a basic entry script and saves it to a file named score.py:
10588

10689
```bash
10790
echo -e 'import time\ndef init(): \n\ttime.sleep(1) \n\ndef run(input_data): \n\treturn {"message":"Hello, World!"}' > score.py
10891
```
10992

110-
1. Start the server with the `azmlinfsrv` command and set the _score.py_ file as the entry script:
111-
112-
<!-- Reviewer: The following command doesn't fully succeed.
113-
I'll add a screenshot of the output in the PR comments. -->
93+
1. Use the `azmlinfsrv` command to start the server and set the score.py file as the entry script:
11494

11595
```bash
11696
azmlinfsrv --entry_script score.py
@@ -119,9 +99,7 @@ To debug your scoring script locally, you have several options for testing the s
11999
> [!NOTE]
120100
> The server is hosted on 0.0.0.0, which means it listens to all IP addresses of the hosting machine.
121101
122-
1. Send a scoring request to the server by using the `curl` utility:
123-
124-
<!-- Reviewer: Because the previous command doesn't fully succeed, I can't verify the output. -->
102+
1. Use the `curl` utility to send a scoring request to the server:
125103

126104
```bash
127105
curl -p 127.0.0.1:5001/score
@@ -133,121 +111,130 @@ To debug your scoring script locally, you have several options for testing the s
133111
{"message": "Hello, World!"}
134112
```
135113

136-
1. After testing, select Ctrl + C to terminate the server.
114+
1. After testing, select **Ctrl**+**C** to stop the server.
137115

138-
Now you can modify the scoring script file (_score.py_) and test your changes by running the server again with the `azmlinfsrv --entry_script score.py` command.
116+
You can modify the score.py scoring script file and test your changes by using the `azmlinfsrv --entry_script score.py` command to run the server again.
139117

140118
### Integrate with Visual Studio Code
141119

142-
To use VS Code and the [Python Extension](https://marketplace.visualstudio.com/items?itemName=ms-python.python) for debugging with the [azureml-inference-server-http](https://pypi.org/project/azureml-inference-server-http/) package, you can use the [Launch and Attach modes](https://code.visualstudio.com/docs/editor/debugging#_launch-versus-attach-configurations).
143-
144-
- For **Launch mode**, set up the _launch.json_ file in VS Code and start the Azure Machine Learning inference HTTP server within VS Code:
145-
146-
1. Start VS Code and open the folder containing the script (_score.py_).
120+
In VS Code, you can use the [Python extension](https://marketplace.visualstudio.com/items?itemName=ms-python.python) for debugging with the [azureml-inference-server-http](https://pypi.org/project/azureml-inference-server-http/) package. VS Code offers two modes for debugging: [launch and attach](https://code.visualstudio.com/docs/editor/debugging#_launch-versus-attach-configurations).
147121

148-
1. Add the following configuration to the _launch.json_ file for that workspace in VS Code:
122+
Before you use either mode, install the `azureml-inference-server-http` package by running the following command:
149123

150-
**launch.json**
151-
152-
```json
153-
{
154-
"version": "0.2.0",
155-
"configurations": [
156-
{
157-
"name": "Debug score.py",
158-
"type": "python",
159-
"request": "launch",
160-
"module": "azureml_inference_server_http.amlserver",
161-
"args": [
162-
"--entry_script",
163-
"score.py"
164-
]
165-
}
166-
]
167-
}
168-
```
124+
```bash
125+
python -m pip install azureml-inference-server-http
126+
```
169127

170-
1. Start the debugging session in VS Code by selecting **Run** > **Start Debugging** or use the keyboard shortcut F5.
128+
> [!NOTE]
129+
> To avoid package conflicts, install the inference HTTP server in a virtual environment. You can use the `pip install virtualenv` command to turn on virtual environments for your configuration.
130+
131+
#### Launch mode
132+
133+
For launch mode, take the following steps to set up the VS Code launch.json configuration file and start the Azure Machine Learning inference HTTP server within VS Code:
134+
135+
1. Start VS Code and open the folder that contains the score.py script.
136+
137+
1. For that workspace in VS Code, add the following configuration to the launch.json file:
138+
139+
```json
140+
{
141+
"version": "0.2.0",
142+
"configurations": [
143+
{
144+
"name": "Debug score.py",
145+
"type": "debugpy",
146+
"request": "launch",
147+
"module": "azureml_inference_server_http.amlserver",
148+
"args": [
149+
"--entry_script",
150+
"score.py"
151+
]
152+
}
153+
]
154+
}
155+
```
171156

172-
- For **Attach mode**, start the Azure Machine Learning inference HTTP server in a command window, and use VS Code with the Python Extension to attach to the process:
157+
1. Start the debugging session in VS Code by selecting **Run** > **Start Debugging** or by selecting **F5**.
173158

174-
> [!NOTE]
175-
> For Linux, first install the `gdb` package by running the `sudo apt-get install -y gdb` command.
159+
#### Attach mode
176160

177-
1. Add the following configuration to the _launch.json_ file for that workspace in VS Code:
178-
179-
**launch.json**
161+
For attach mode, take the following steps to use VS Code with the Python extension to attach to the Azure Machine Learning inference HTTP server process:
180162

181-
```json
182-
{
183-
"version": "0.2.0",
184-
"configurations": [
185-
{
186-
"name": "Python: Attach using Process Id",
187-
"type": "python",
188-
"request": "attach",
189-
"processId": "${command:pickProcess}",
190-
"justMyCode": true
191-
}
192-
]
193-
}
194-
```
163+
> [!NOTE]
164+
> For Linux, first install the `gdb` package by running the `sudo apt-get install -y gdb` command.
165+
166+
1. Start VS Code and open the folder that contains the score.py script.
167+
168+
1. For that workspace in VS Code, add the following configuration to the launch.json file:
169+
170+
```json
171+
{
172+
"version": "0.2.0",
173+
"configurations": [
174+
{
175+
"name": "Python: Attach using Process Id",
176+
"type": "debugpy",
177+
"request": "attach",
178+
"processId": "${command:pickProcess}",
179+
"justMyCode": true
180+
}
181+
]
182+
}
183+
```
195184

196-
1. In a command window, start the inference HTTP server by using the `azmlinfsrv --entry_script score.py` command.
185+
1. In a command window, start the inference HTTP server by running the `azmlinfsrv --entry_script score.py` command.
197186

198-
1. Start the debugging session in VS Code:
187+
1. Take the following steps to start the debugging session in VS Code:
199188

200-
1. Select **Run** > **Start Debugging** or use the keyboard shortcut F5.
189+
1. Select **Run** > **Start Debugging**, or select **F5**.
201190

202-
1. In the command window, view the logs from the inference server and locate the process ID of the `azmlinfsrv` command (not the `gunicorn`):
191+
1. In the command window, search the logs from the inference server to locate the process ID of the `azmlinfsrv` process, not the `gunicorn` process:
203192

204-
:::image type="content" source="./media/how-to-inference-server-http/debug-attach-pid.png" border="false" alt-text="Screenshot that shows a command window displaying logs from the inference HTTP server and the process ID of the azmlinfsrv command highlighted.":::
193+
:::image type="content" source="./media/how-to-inference-server-http/debug-attach-pid.png" border="false" alt-text="Screenshot of a command window that shows inference HTTP server logs. In one log statement, the process ID of the azmlinfsrv command is highlighted.":::
205194

206-
1. In the VS Code Debugger, enter the process ID of the `azmlinfsrv` command.
195+
1. In the VS Code debugger, enter the process ID of the `azmlinfsrv` process.
207196

208-
If you don't see the VS Code process picker, you can manually enter the process ID in the `processId` field of the _launch.json_ file for that workspace.
197+
If you don't see the VS Code process picker, manually enter the process ID in the `processId` field of the launch.json file for the workspace.
209198

210-
For both modes, you can set [breakpoints](https://code.visualstudio.com/docs/editor/debugging#_breakpoints) and debug the script step by step.
199+
For launch and attach modes, you can set [breakpoints](https://code.visualstudio.com/docs/editor/debugging#_breakpoints) and debug the script step by step.
211200

212201
### Use an end-to-end example
213202

214203
The following procedure runs the server locally with [sample files](https://github.com/Azure/azureml-examples/tree/main/cli/endpoints/online/model-1) (scoring script, model file, and environment) from the Azure Machine Learning example repository. For more examples of how to use these sample files, see [Deploy and score a machine learning model by using an online endpoint](how-to-deploy-online-endpoints.md).
215204

216-
1. Clone the sample repository:
205+
1. Clone the sample repository and go to the folder that contains the relevant sample files:
217206

218207
```bash
219208
git clone --depth 1 https://github.com/Azure/azureml-examples
220209
cd azureml-examples/cli/endpoints/online/model-1/
221210
```
222211

223-
1. Create and activate a virtual environment with [conda](https://conda.io/projects/conda/en/latest/user-guide/getting-started.html):
212+
1. Use [conda](https://conda.io/projects/conda/en/latest/user-guide/getting-started.html) to create and activate a virtual environment:
224213

225-
In this example, the `azureml-inference-server-http` package is automatically installed. The package is included as a dependent library of the `azureml-defaults` package in the _conda.yaml_ file:
214+
In this example, the `azureml-inference-server-http` package is automatically installed. The package is included as a dependent library of the `azureml-defaults` package in the conda.yaml file:
226215

227216
```bash
228-
# Create the environment from the YAML file
217+
# Create the environment from the YAML file.
229218
conda env create --name model-env -f ./environment/conda.yaml
230-
# Activate the new environment
219+
# Activate the new environment.
231220
conda activate model-env
232221
```
233222

234-
1. Review your scoring script:
235-
236-
**onlinescoring/score.py**
223+
1. Review the scoring script, onlinescoring/score.py:
237224

238225
:::code language="python" source="~/azureml-examples-main/cli/endpoints/online/model-1/onlinescoring/score.py":::
239226

240-
1. Run the inference HTTP server by specifying the scoring script and model file:
227+
1. Run the inference HTTP server by specifying the scoring script and model file.
241228

242-
The model directory specified in the `model_dir` parameter is defined by using the `AZUREML_MODEL_DIR` variable and retrieved in the scoring script.
229+
The model directory specified in the `model_dir` parameter is defined by using the `AZUREML_MODEL_DIR` variable and retrieved in the scoring script.
243230

244-
In this case, you specify the current directory _./_ because the subdirectory is specified in the scoring script as _model/sklearn\_regression\_model.pkl_.
231+
In this case, you use the current directory, `./`, as the `model_dir` value, because the scoring script specifies the subdirectory as `model/sklearn\_regression\_model.pkl`.
245232

246233
```bash
247234
azmlinfsrv --entry_script ./onlinescoring/score.py --model_dir ./
248235
```
249236

250-
When the server launches and successfully invokes the scoring script, the example [startup log](#view-startup-logs) opens. Otherwise, the log shows error messages.
237+
When the server starts and successfully invokes the scoring script, the example [startup log](#view-startup-logs) opens. Otherwise, the log shows error messages.
251238

252239
1. Test the scoring script with sample data:
253240

0 commit comments

Comments
 (0)