Deploying HANA ML with SAP Kyma Serverless Functions

Introduction

In this blogpost I will walk through quick and easy low-code steps to use the capabilities provided by SAP Kyma Serverless Functions to deploy HANA Machine Learning libraries using python API client.

This deployment method is useful mostly in such cases where you do not want the overhead of maintaining dedicated docker images and docker repository management.

For details on use of Kyma functions refer to SAP Help Documentation for Kyma Functions.

This functionality is useful when most of the workload is done on the HANA Cloud layer for training or inferencing a machine learning model and the user wants to provide an API to trigger the training or inference call. This step is typically needed after the model development has been done and an endpoint is needed to simply call the inference.

If the code requires significant dependencies on other libraries for data preparation and feature selection then a full deployment on Kyma is recommended. Here are some sample applications which can be used SAP-samples Kyma runtime-extension-samples as starters. In these cases the docker image needs to have hana-ml python package.

The same functionality can also be achieved via kubectl functionality, here we use the Kyma Cockpit and see the ease of creating a serverless function. For a developer focused functionality there are jumpstart generators for VS Code which enable the same. Here is a link to a complete tutorial from SAP HANA Academy SAP HANA Academy for BTP Serverless Python.

Pre-requisites

To go through the the steps you would need to ensure you already have the following:

  1. You have HANA Cloud Database instance and required credentials to connect. This could be either HANA Cloud or underlying HANA Cloud from a SAP Datasphere tenant.
  2. Setup BTP Account with SAP Kyma runtime as described here Create Kyma Environment on BTP 

Create Kyma Function to access HANA ML via Python API Client

Choose the Kyma namespace where you would like to deploy this functionality

  1. Add the HANA credentials, in our example, hanaauth, required to connect to the Secrets in KymaAdd%20Secret%20to%20Kyma
  2. Go to Workloads and  Create FunctionCreate%20Kyma%20FunctionThis basic version works with Function profile XS but incase you have higher memory or CPU requirements for building (incase of other additional libraries) or compute increase the resources requirements.
  3. Add Environment variables using the Secret
  4. To enable the use of hanaml libraries we need to
    1. Add the hana-ml and shapely in the dependencies
    2. Go to the yaml view of the  file and add runtimeImageOverride: hanaacademy/kyma-faas:python39 after runtime:python39. This is the most critical step as without the runtime override the Kyma function is not able to build with hana-ml dependency as the default base image does not support it.
  runtime: python39
  runtimeImageOverride: hanaacademy/kyma-faas:python39

Edit%20Deployment%20file%20in%20yaml%20mode

Edit Deployment file in yaml mode

                              Here is some sample code to test the hana ml connectivity works. The specific ML code required can be added here depending on whether its a training or inferencing run.
import hana_ml
import os
def connectToHANA():
    import hana_ml.dataframe as dataframe
    conn = dataframe.ConnectionContext(address = os.environ.get('HANA_ADDRESS'),
                                    port = os.environ.get('HANA_PORT'),
                                    user = os.environ.get('HANA_USER'),
                                    password = os.environ.get('HANA_PASSWORD'),
                                    encrypt = 'true')
    print("HANA DB version:", conn.hana_version())
                  

def main(event, context):
    message = 'Hello World from the Kyma Function '+context['function-name']+' running on '+context['runtime']+ "hana_ml version:" + hana_ml.__version__ + '!';
    print(message)
    connectToHANA()
    print("hana_ml version:" + hana_ml.__version__)
    return message

Create API rule to call the Kyma function for HANA ML

Once the function is created we need to add an API rule to ensure access outside the Kyma cluster as usual

For this go to the Discovery and Network section -> API Rules

Creating the API rule is straightforward, you can change the Name as you like and provide a Subdomain which adds a prefix to the host and helps to distinguish this service from others you may create on the Kyma cluster

Create%20API%20Rule

Create API Rule

 

The above endpoint can now be integrated in the end-user application. Typically you would then add authorization steps which can be done in Kyma. For example this endpoint can also be called from  SAP Analytics Cloud via API Step in SAC Multi Actions to trigger workloads on SAP HANA.

Testing and Debugging the endpoint

You can test the endpoint by calling it directly from the browser or via postman and check the logs in Kyma console of the corresponding pod.

Sample%20Logs%20from%20Kyma%20Pods

Sample Logs from Kyma Pods

 

Original Article:
https://blogs.sap.com/2023/04/19/deploying-hana-ml-with-sap-kyma-serverless-functions/

ASK SAP EXPERTS ONLINE
Related blogs

LEAVE A REPLY

Please enter your comment!
Please enter your name here