Bringing code analysis tools to Jupyter notebooks

Based on a survey of thousands of machine learning practitioners, a new CodeGuru extension addresses common problems, such as code cell execution order, incorrect API calls, and security.

The computational notebook is an interactive, web-based programming interface based on the concept of a lab notebook. Users can describe the computations they’re performing — including diagrams — and embed code in the notebook, and the notebook backend will execute the code, integrating the results into the notebook layout.

Jupyter Notebook is the most popular implementation of computational notebooks, and it has become the tool of choice for data scientists. By September 2018, there were more than 2.5 million public Jupyter notebooks on GitHub, and this number has been growing rapidly.

Related content
In a pilot study, an automated code checker found about 100 possible errors, 80% of which turned out to require correction.

However, using Jupyter Notebook poses several challenges related to code maintenance and machine learning best practices. We recently surveyed 2,669 machine learning (ML) practitioners, and 33% of them mentioned that notebooks get easily cluttered due to the mix of code, documentation, and visualization. Similarly, 23% found silent bugs hard to detect, and 18% agreed that global variables are inconsistently used. Another 15% found reproduction of notebooks to be hard, and 6% had difficulty detecting and remediating security vulnerabilities within notebooks.

We are excited to share our recent launch of the Amazon CodeGuru extension for JupyterLab and SageMaker Studio. The extension seamlessly integrates with JupyterLab and SageMaker Studio, and with a single button click, it can provide users feedback and suggestions for improving their code quality and security. To learn more about how to install and use this extension, check out this user guide.

Static analysis

Traditional software development environments commonly use static-analysis tools to identify and prevent bugs and enforce coding standards, but Jupyter notebooks currently lack such tools. We on the Amazon CodeGuru team, which has developed a portfolio of code analysis tools for Amazon Web Services customers, saw a great opportunity to adapt our existing tools for notebooks and build solutions that best fit this new problem area.

Notebook-Interface.jpg
An example of how the notebook environment can integrate discussion, code, and visualizations.

We presented our initial efforts in a paper published at the 25th International Symposium on Formal Methods in March 2023. The paper reports insights from our survey and from interviews with ML practitioners to understand what specific issues need to be addressed in the notebook context. In the following, we give two examples of how our new technologies can help machine learning experts to be more productive.

Execution order

Code is embedded in computational notebooks in code cells, which can be executed in an arbitrary order and edited on the fly; that is, cells can be added, deleted, or changed after other cells have been executed.

While this flexibility is great for exploring data, it raises problems concerning reproducibility, as cells with shared variables can produce different results when running in different orders.

Jupyter code examples.png
Left: code cells executed in nonlinear order; right: code cells executed in linear order.

Once a code cell is executed, it is assigned an integer number in the square bracket on its left side. This number is called the execution count, and it indicates the cell’s position in the execution order. In the example above, when code cells are executed in nonlinear order, the variable z ends up with the value 6. However, execution count 2 is missing in the notebook file, which can happen for multiple reasons: perhaps the cell was executed and deleted afterwards, or perhaps one of the cells was executed twice. In any case, it would be hard for a second person to reproduce the same result.

Related content
New tool can spot problems — such as overfitting and vanishing gradients — that prevent machine learning models from learning.

To catch problems resulting from out-of-order execution in Jupyter notebooks, we developed a hybrid approach that combines dynamic information capture and static analysis. Our tool collects dynamic information during the execution of notebooks, then converts notebook files with Python code cells into a novel Python representation that models the execution order as well as the code cells as such. Based on this model, we are able to leverage our static-analysis engine for Python and design new static-analysis rules to catch issues in notebooks.

APIs

Another common problem for notebook users is misuse of machine-learning APIs. Popular machine learning libraries such as PyTorch, TensorFlow, and Keras greatly simplify the development of AI systems. However, due to the complexity of the field, the libraries’ high level of abstraction, and the sometimes obscure conventions governing library functions, library users often misuse these APIs and inject faults into their notebooks without even knowing it.

Related content
ICSE paper presents techniques piloted by Amazon Web Services’ Automated Reasoning team.

The code below shows such a misuse. Some layers of a neural network, such as dropout layers, may behave differently during the training and evaluation of the network. PyTorch mandates explicit calls to train() and eval() to denote the start of training and evaluation, respectively. The code example is intended to load a trained model from disk and evaluate it on some test data.

However, it misses the call to eval(), as by default, every model is in the training phase. In this case, some layers will indirectly change the architecture of the network, which would make all prediction unstable; i.e., for the same input, the predictions would be different at different times.

# noncompliant case
model.load_state_dict(torch.load("model.pth"))
predicted = model.evaluate_on(test_data)

# compliant case
model.load_state_dict(torch.load("model.pth"))
model.eval()
predicted = model.evaluate_on(test_data)

Instabilities caused by this bug can have a serious impact. Even when the bug is found (currently, through manual code review) and fixed, the model needs to be retrained. Depending on how large the model is and how late in the development process the bug is found, this could mean a waste of thousands of hours.

The best case would be to detect the bug directly after the developer writes the code. Static analysis can help with this. In our paper, we implemented a set of static-analysis rules that automatically analyze machine learning code in Jupyter notebooks and could detect such bugs with high precision.

In experiments involving a large set of notebook files, our rules found an average of one bug per seven notebooks. This result motivates us to dive deep into bug detection in Jupyter notebooks.

Our survey identified the following issues that notebook users care about:

  • Reproducibility: People often find it difficult to reproduce results when moving notebooks between different environments. Notebook code cells are often executed in nonlinear order, which may be not reproducible. About 14% of the survey participants collaborate on notebooks with others only when models need to be pushed into production; reproducibility is even more crucial for production notebooks.
  • Correctness: People introduce silent correctness bugs without knowing it when using machine learning libraries. Silent bugs affect model outputs but do not cause program crashes, which makes them extremely hard to find. In our survey, 23% of participants confirmed this.
  • Readability: During data exploration, notebooks can easily get messy and hard to read. This hampers maintainability as well as collaboration. In our survey, 32% of participants mentioned that readability is one of the biggest difficulties in using notebooks.
  • Performance: It is time- and memory-consuming to train big models. People want help to make both training and the runtime execution of their code more efficient.
  • Security: In our survey, 34% of participants said that security awareness among ML practitioners is low and that there is a consequent need for security scanning. Because notebooks often rely on external code and data, they can be vulnerable to code injection and data-poisoning attacks (manipulating machine learning models).

These findings pointed us toward the kinds of issues that our new analysis rules should address. During the rule sourcing and specification phase, we asked ML experts for feedback on the usefulness of the rules as well as examples of compliant and noncompliant cases to illustrate the rules. After developing the rules, we invited a group of ML experts to evaluate our tools on real-world notebooks. We used their feedback to improve the accuracy of the rules.

The newly launched Amazon CodeGuru extension for JupyterLab and SageMaker Studio enables the enforcement of code quality and security in computational notebooks to “shift left”, or move earlier in the development process. Users can now detect security vulnerabilities — such as injection flaws, data leaks, weak cryptography, and missing encryption — within notebook cells, along with other common issues that affect the readability, reproducibility, and correctness of the computations performed by notebooks.

Acknowledgements: Martin Schäf, Omer Tripp

Research areas

Related content

US, WA, Seattle
Amazon continues to invest heavily in building our world class advertising business. Our products are strategically important to our Retail and Marketplace businesses, driving long term growth. We deliver billions of ad impressions and millions of clicks daily, breaking fresh ground to create world-class products. We are highly motivated, collaborative and fun-loving with an entrepreneurial spirit and strong bias for action. With a broad mandate to experiment and innovate, we are growing at an unprecedented rate with a seemingly endless range of new opportunities. The Sponsored Products Monetization team is broadly responsible for pricing of ads on Amazon search pages, balancing short-term and long-term ad revenue growth to drive sustainable marketplace health. As a Senior Applied Scientist on our team, you will be responsible for defining the science and technical strategy for one of our most impactful marketplace controls, creating lasting value for Amazon and our advertising customers. You will help to identify unique opportunities to create customized and delightful shopping experience for our growing marketplaces worldwide. Your job will be identify big opportunities for the team that can help to grow Sponsored Products business working with retail partner teams, Product managers, Software engineers and PMs. You will have opportunity to design, run and analyze A/B experiments to improve the experience of millions of Amazon shoppers while driving quantifiable revenue impact. More importantly, you will have the opportunity to broaden your technical skills in an environment that thrives on creativity, experimentation, and product innovation. Key job responsibilities - Lead science, tech and business strategy and roadmap for Sponsored Products Monetization - Drive alignment across multiple organizations for science, engineering and product strategy to achieve business goals - Lead and mentor scientists and engineers across teams to develop, test, launch and improve of science models designed to optimize the shopper experience and deliver long term value for Amazon and advertisers - Develop state of the art experimental approaches and ML models - Drive end-to-end Machine Learning projects that have a high degree of ambiguity, scale, complexity - Establish scalable, efficient, automated processes for large-scale data analysis, machine-learning model development, model validation and serving - Research new and innovative machine learning approaches - Recruit Scientists to the team and provide mentorship
US, WA, Bellevue
We are a part of Amazon Alexa organization where our mission is “delight customers through contextual and personalized proactive experiences that keep customers informed, engaged, and productive without cognitive burden”. We are developing advanced systems to deliver engaging, intuitive, and adaptive content recommendations across all Amazon surfaces. We aim to facilitate seamless reasoning and customer experiences, surpassing the capabilities of previous machine learning models. We are looking for a passionate, talented, and resourceful Senior Applied Scientist in the field of Natural Language Processing (NLP), Large Language Model (LLM), Recommender Systems and/or Information Retrieval, to invent and build scalable solutions for a state-of-the-art context-aware personal assistant. A successful candidate will have strong machine learning background and a desire to push the envelope in one or more of the above areas. The ideal candidate would also enjoy operating in dynamic environments, be self-motivated to take on challenging problems to deliver big customer impact, shipping solutions via rapid experimentation and then iterating on user feedback and interactions. Key job responsibilities As a Senior Applied Scientist, you will leverage your technical expertise and experience to demonstrate leadership in tackling large complex problems, setting the direction and collaborating with applied scientists and engineers to develop novel algorithms and modeling techniques to enable timely, relevant and delightful recommendations and conversations. Your work will directly impact our customers in the form of products and services that make use of various machine learing, deep learning and language model technologies. You will leverage Amazon’s heterogeneous data sources and large-scale computing resources to accelerate advances in the state of art.
US, CA, East Palo Alto
The Applied Scientist will play a critical role in the research, develop, and implementation of solutions to key challenges in developing conversational AI systems that can understand and communicate with customers in a natural and contextually appropriate manner. This involves tackling complex problems in areas such as multi-turn dialogue management, knowledge grounding, and open-ended generation. Key job responsibilities 1. Research and development of LLM-based chatbots and conversational AI systems for customer service applications. 2. Design and implement state-of-the-art NLP and ML models for tasks such as language understanding, dialogue management, and response generation. 3. Collaborate with cross-functional teams, including data scientists, software engineers, and product managers, to integrate LLM-based solutions into Amazon's customer service platforms. 4. Develop and implement strategies for data collection, annotation, and model training to ensure high-quality and robust performance of the chatbots. 5. Conduct experiments and evaluations to measure the performance of the developed models and systems, and identify areas for improvement. 6. Stay up-to-date with the latest advancements in NLP, LLMs, and conversational AI, and explore opportunities to incorporate new techniques and technologies into Amazon's customer service solutions. 7. Collaborate with internal and external research communities, participate in conferences and publications, and contribute to the advancement of the field. A day in the life We thrive on solving challenging problems to innovate for our customers. By pushing the boundaries of technology, we create unparalleled experiences that enable us to rapidly adapt in a dynamic environment. Our decisions are guided by data, and we collaborate with engineering, science, and product teams to foster an innovative learning environment. If you are not sure that every qualification on the list above describes you exactly, we'd still love to hear from you! At Amazon, we value people with unique backgrounds, experiences, and skillsets. If you’re passionate about this role and want to make an impact on a global scale, please apply! Benefits Summary: Amazon offers a full range of benefits that support you and eligible family members, including domestic partners and their children. Benefits can vary by location, the number of regularly scheduled hours you work, length of employment, and job status such as seasonal or temporary employment. The benefits that generally apply to regular, full-time employees include: 1. Medical, Dental, and Vision Coverage 2. Maternity and Parental Leave Options 3. Paid Time Off (PTO) 4. 401(k) Plan About the team Join our team of scientists and engineers who develop and deploy LLM-based Conversational AI systems to enhance Amazon's customer service experience and effectiveness. We work on innovative solutions that help customers solve their issues and get their questions answered efficiently, and associate-facing products that support our customer service associate workforce.
US, MA, Boston
As part of Alexa CAS team, our mission is to create a best-in-class Conversational AI that is intuitive, intelligent, and responsive, by developing superior Large Language Models (LLM) solutions and services which increase the capabilities built into the model and which enable utilizing thousands of APIs and external knowledge sources to provide the best experience for each request across millions of customers and endpoints. We are looking for a passionate, talented, and resourceful Applied Scientist in the field of LLM, Artificial Intelligence (AI), Natural Language Processing (NLP), Recommender Systems and/or Information Retrieval, to invent and build scalable solutions for a state-of-the-art context-aware conversational AI. A successful candidate will have strong machine learning background and a desire to push the envelope in one or more of the above areas. The ideal candidate would also have hands-on experiences in building Generative AI solutions with LLMs, enjoy operating in dynamic environments, be self-motivated to take on challenging problems to deliver big customer impact, moving fast to ship solutions and then iterating on user feedback and interactions. Key job responsibilities As an Applied Scientist, you will leverage your technical expertise and experience to collaborate with other talented applied scientists and engineers to research and develop novel algorithms and modeling techniques to reduce friction and enable natural and contextual conversations. You will analyze, understand and improve user experiences by leveraging Amazon’s heterogeneous data sources and large-scale computing resources to accelerate advances in artificial intelligence. You will work on core LLM technologies, including Supervised Fine-Tuning (SFT), In-Context Learning (ICL), Learning from Human Feedback (LHF), etc. Your work will directly impact our customers in the form of novel products and services . Key job responsibilities . You will analyze, understand and improve user experiences by leveraging Amazon’s heterogeneous data sources and large-scale computing resources to accelerate advances in artificial intelligence. . You will work on core LLM technologies, including developing best-in-class modeling, prompt optimization algorithms to enable Conversation AI use cases · Build and measure novel online & offline metrics for personal digital assistants and customer scenarios, on diverse devices and endpoints · Create, innovate and deliver deep learning, policy-based learning, and/or machine learning based algorithms to deliver customer-impacting results · Perform model/data analysis and monitor metrics through online A/B testing · Research and implement novel machine learning and deep learning algorithms and models.
US, WA, Seattle
The Alexa Smart Home team is focused on making Alexa the user interface for the home. From the simplest voice commands (turn on the lights, turn down the heat) to use cases spanning home security, home entertainment, and the home environment; we are evolving Alexa into an intelligent, indispensable companion that automates daily routines, simplifies interaction with appliances and electronics, and alerts when something unusual is detected. You can be part of a team delivering features that are highly anticipated by media and well received by our customers. As an Applied Scientist, you will work with other scientists and software developers to design and build the next generation of Smart Home voice control using the latest Large Language Models (LLMs). And, you will have the satisfaction of working on a product your friends and family can relate to, and want to use every day. Key job responsibilities - Develop new inference and training techniques to improve the performance of LLMs for Smart Home control and Automation - Develop robust techniques for synthetic data generation for training large models and maintaining model generalization - Mentoring junior scientists to improve their skills, knowledge, and their ability to get things done About the team We are a team of Scientists, Machine Learning Engineers, and Software Developers that work together to make Alexa more insightful and proactive through ambient intelligence, with features like Alexa Hunches that automatically control Smart Home devices. We are interdisciplinary and we act like it. We ask each other questions and value our different perspectives.
IN, KA, Bangalore
AWS Sales, Marketing, and Global Services (SMGS) is responsible for driving revenue, adoption, and growth from the largest and fastest growing small- and mid-market accounts to enterprise-level customers including public sector. The AWS Global Support team interacts with leading companies and believes that world-class support is critical to customer success. AWS Support also partners with a global list of customers that are building mission-critical applications on top of AWS services. Do you have proven analytical capabilities to identify business opportunities, develop predictive models and optimization algorithms to help us build state of the art Support organization? At Amazon, we are working to be the most customer-centric company on earth. To get there, we need exceptionally talented, bright, and driven people. We set big goals and are looking for people who can help us reach and exceed them. Amazon Web Services (AWS) is one of the world’s most comprehensive and broadly adopted cloud platform, offering over 200 fully featured services from data centers globally. Amazon Web Services, Inc. provides services for broad range of applications including compute, storage, databases, networking, analytics, machine learning and artificial intelligence (AI), Internet of Things (IoT), security, and application development, deployment, and management. Global AWS Support BizOPs team is looking for a passionate Data Scientist to model contact forecasting, discovering insights and identifying opportunities through the use of statistics, machine learning, and deep learning to drive business and operational improvements. A successful candidate must be passionate about building solutions that will help drive a more efficient operations network and optimize cost. In this role, you will partner with data engineering, Tooling team, operations, Training, Customer Service, Capacity planning and finance teams, driving optimization and prediction solutions across the network. Key job responsibilities We are looking for an experienced and motivated Sr.Data Scientist with proven abilities to build and manage modeling projects, identify data requirements, build methodology and tools that are statistically grounded The candidate will be an expert in the areas of data science, optimization, machine learning and statistics, and is comfortable facilitating ideation and working from concept through execution. The candidate is customer obsessed, innovative, independent, results-oriented and enjoys working in a fast-paced growing organization. An interest in operations, manufacturing or process improvement is helpful. The ability to embrace this ambiguity and work with a highly distributed team of experts is critical. As we scale up, there is opportunity to own globally impactful work and grow your career in technical, programmatic or people leadership. You will likely work with Python or R, though specific particular modelling language. Your problem-solving ability, knowledge of data models and ability to drive results through ambiguity are more important to us. A day in the life Diverse Experiences AWS values diverse experiences. Even if you do not meet all of the qualifications and skills listed in the job description, we encourage candidates to apply. If your career is just starting, hasn’t followed a traditional path, or includes alternative experiences, don’t let it stop you from applying. Why AWS? Amazon Web Services (AWS) is the world’s most comprehensive and broadly adopted cloud platform. We pioneered cloud computing and never stopped innovating — that’s why customers from the most successful startups to Global 500 companies trust our robust suite of products and services to power their businesses. Inclusive Team Culture Here at AWS, it’s in our nature to learn and be curious. Our employee-led affinity groups foster a culture of inclusion that empower us to be proud of our differences. Ongoing events and learning experiences, including our Conversations on Race and Ethnicity (CORE) and AmazeCon (gender diversity) conferences, inspire us to never stop embracing our uniqueness. Mentorship & Career Growth We’re continuously raising our performance bar as we strive to become Earth’s Best Employer. That’s why you’ll find endless knowledge-sharing, mentorship and other career-advancing resources here to help you develop into a better-rounded professional. Work/Life Balance We value work-life harmony. Achieving success at work should never come at the expense of sacrifices at home, which is why we strive for flexibility as part of our working culture. When we feel supported in the workplace and at home, there’s nothing we can’t achieve in the cloud.
US, CA, San Francisco
If you are interested in this position, please apply on Twitch's Career site https://www.twitch.tv/jobs/en/ About Us: Twitch is the world’s biggest live streaming service, with global communities built around gaming, entertainment, music, sports, cooking, and more. It is where thousands of communities come together for whatever, every day. We’re about community, inside and out. You’ll find coworkers who are eager to team up, collaborate, and crush (or elegantly solve) problems together. We’re on a quest to empower live communities, so if this sounds good to you, see what we’re up to on LinkedIn and X, and discover the projects we’re solving on our Blog. Be sure to explore our Interviewing Guide to learn how to ace our interview process. About the Role: We are looking for an experienced Data Scientist to support our central analytics and finance disciplines at Twitch. Bringing to bear a mixture of data analysis, dashboarding, and SQL query skills, you will use data-driven methods to answer business questions, and deliver insights that deepen understanding of our viewer behavior and monetization performance. Reporting to the Head of Finance, Analytics, and Business Operations, your team will be located in San Francisco. While there is a preference for the San Francisco Bay Area. You Will: - Create actionable insights from data related to Twitch viewers, creators, advertising revenue, commerce revenue, and content deals. - Develop dashboards and visualizations to communicate points of view that inform business decision-making. - Create and maintain complex queries and data pipelines for ad-hoc analyses. - Author narratives and documentation that support conclusions. - Collaborate effectively with business partners, product managers, and data team members to align data science efforts with strategic goals.
US, WA, Seattle
The Private Brands Discovery team designs innovative machine learning solutions to enhance customer awareness of Amazon’s own brands and help customers find products they love. This interdisciplinary team of scientists and engineers incubates and develops disruptive solutions using cutting-edge technology to tackle some of the most challenging scientific problems at Amazon. To achieve this, the team utilizes methods from Natural Language Processing, deep learning, large language models (LLMs), multi-armed bandits, reinforcement learning, Bayesian optimization, causal and statistical inference, and econometrics to drive discovery throughout the customer journey. Our solutions are crucial to the success of Amazon’s private brands and serve as a model for discovery solutions across the company. This role presents a high-visibility opportunity for someone eager to make a business impact, delve into large-scale problems, drive measurable actions, and collaborate closely with scientists and engineers. As a team lead, you will be responsible for developing and coaching talent, guiding the team in designing and developing cutting-edge models, and working with business, marketing, and software teams to address key challenges. These challenges include building and improving models for sourcing, relevance, and CTR/CVR estimation, deploying reinforcement learning methods in production etc. In this role, you will be a technical leader in applied science research with substantial scope, impact, and visibility. A successful team lead will be an analytical problem solver who enjoys exploring data, leading problem-solving efforts, guiding the development of new frameworks, and engaging in investigations and algorithm development. You should be capable of effectively interfacing between technical teams and business stakeholders, pushing the boundaries of what is scientifically possible, and maintaining a sharp focus on measurable customer and business impact. Additionally, you will mentor and guide scientists to enhance the team's talent and expand the impact of your work.
US, MA, Boston
* Note: This job is located in Hudson, MA Amazon Dash Cart allows shoppers to checkout without lines — you just place the items in the cart and the cart will take care of the rest. Designed and custom-built by Amazonians, our Dash Cart uses a variety of technologies including computer vision, sensor fusion, and advanced machine learning. Check it out at https://www.amazon.com/b?ie=UTF8&node=21289116011. We are looking for an Applied Scientist to develop hardware solutions that require significant innovation for our Amazon Dash Cart team, located in Hudson, MA. As an Applied Scientist within the hardware development team, you will engage with a skilled and accomplished cross-disciplinary staff to conceive and evaluate innovative technologies. You will collaborate with internal and external stakeholders to drive key aspects of technology solution definition, execution and validation. Key job responsibilities - Evaluate or conceive of new cameras, sensors, and computer vision systems which push the limit of existing technologies and delight Dash Cart customers. - Design embedded compute architectures optimized for cost and power efficiency. - Propose hardware solutions and create working prototypes while working with hardware development engineers to bring those prototypes to production. - Develop computer vision algorithms including ISP optimization and video pipelines architectures. - Develop firmware device drivers for interfacing to a range of hardware components and sensors. - Work closely with an inter-disciplinary product development team including outside partners to bring prototypes into production. - Use machine learning, data mining, statistical techniques and others to create actionable, meaningful, and scalable solutions for the business' problems.
US, CA, Santa Clara
The Geospatial science team solves problems at the interface of ML/AI and GIS for Amazon's last mile delivery programs. We have access to Earth-scale datasets and use them to solve challenging problems that affect hundreds of thousands of transporters. We are looking for strong candidates to join the transportation science team which owns time estimation, GPS trajectory learning, and sensor fusion from phone data. You will join a team of GIS and ML domain experts and be expected to develop ML models, present research results to stakeholders, and collaborate with SDEs to implement the models in production. Key job responsibilities - Understand business problems and translate them into science problems - Develop ML models - Present research results - Write and publish papers - Collaborate with other scientists