<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Daniel Tan on Medium]]></title>
        <description><![CDATA[Stories by Daniel Tan on Medium]]></description>
        <link>https://medium.com/@danieltanfh95?source=rss-6f800b737df0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ceUdZvy46urE_yk9QVzLNg.jpeg</url>
            <title>Stories by Daniel Tan on Medium</title>
            <link>https://medium.com/@danieltanfh95?source=rss-6f800b737df0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 21:47:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@danieltanfh95/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Nobody on the internet knows if you are a human]]></title>
            <link>https://medium.com/@danieltanfh95/nobody-on-the-internet-knows-if-you-are-a-human-3e9ada8d2bfd?source=rss-6f800b737df0------2</link>
            <guid isPermaLink="false">https://medium.com/p/3e9ada8d2bfd</guid>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[privacy]]></category>
            <category><![CDATA[security]]></category>
            <dc:creator><![CDATA[Daniel Tan]]></dc:creator>
            <pubDate>Thu, 28 May 2026 12:34:22 GMT</pubDate>
            <atom:updated>2026-05-28T12:34:22.906Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/299/1*idl7RzjbFSaN8SDEARntUQ.jpeg" /><figcaption>Cartoon by Peter Steiner, The New Yorker, July 5, 1993.</figcaption></figure><p>Technology is progressing to the point where it is getting increasingly harder to tell if someone is a bot or a human. LessWrong ironically uses LLM tools to tell LLMs apart from humans. On the internet nobody knows if you are a dog, or a LLM-inspired human. This is nothing new: David Chaum published “Security without Identification: Transaction Systems to Make Big Brother Obsolete” in 1985 to argue that we should build identity systems without actually learning who someone is — to which the internet responded by collectively using username+passwords to build account identities, then got surprise-Pikachu’ed when bots fought through anyway via Sybil attacks.</p><p>The present situation is that the Internet is actively hostile towards good actors (presumed to be humans at this point) through a combination of CAPTCHAs, accounts requiring logging in via your email and your mom’s birthday, or 2FA that requires a trusted third party (often mega-corporations) to escort you into the website, all while being friendly to automation through unofficial APIs or web scraping. Computer use in LLMs has largely slammed the door shut on traditional methods of preventing bots.</p><p>The other approach that largely turned out to be security theater was <a href="https://anubis.techaro.lol/">Anubis</a>, which proudly proclaimed itself the watchdog of the internet, weigher of souls, but in the words of its creator “Over time I thought the proof-of-work was actually doing something for security, but no — any barrier makes the low-effort scrapers confused and give up.” Really, the rapid FOSS adoption (200k downloads to date) has largely just rehashed Hashcash and propagated anime cat girls throughout the internet, besides actively calling out Mozilla as the benevolent god of bots (except that it is not only trivial to bypass, it ignores `curl`). As it turns out, the more valuable your website, the higher the floor of computation you need to use, but at some point you have to pay latency debts back, so you have a ceiling where this works out. People abandoned Hashcash for good reason, it burdens legitimate users while doing nothing to bots who wants to squeeze you for value and provide nothing back.</p><p>The mistake here is largely treating bots (or “clankers” in the ongoing debate between <a href="https://lucumr.pocoo.org/2026/5/24/pi-oss/">boomers</a> and <a href="https://lucumr.pocoo.org/2026/5/26/clankers/">clankers</a> that’s also indicative of the entire misguided approach to this) as “all bad” and humans as “all good”. Bots are a tool that are driven by other humans to leech off your website. I also hate the contrived name “clanker” when we have a perfectly good name for irresponsible agents: bots, which is short, catchy, and has been in use for a long time.</p><p>What we want are really to filter bad from good agents (AI or humans alike) with a third party “substrate” that is independent of our ability to think (as you can see, trying to split AI from humans via the capability and capacity to think has largely ended up in spectacular failure and a total waste of time and resources).</p><p>And we already have it. It is called time.</p><p>For example, it is perfectly acceptable to be suspicious of someone who has moved into your neighbourhood, and similar that person would be suspicious of others in the neighbourhood. It would be weird, in fact, if that person acted overly friendly to people, and vice versa. However, as time passes with normal-to-positive interactions between both parties, respect is earned between both parties, more trust is allocated, and thus more openings, more information is available.</p><p>Hacker News works the same way: a new account can’t downvote or flag until it has earned karma because privileges accrue with proven history, not at signup. Time is the passive, all-knowing, self-historical substrate we can operate trust on.</p><p>This is just common sense we can apply here, and that is what I am doing with <a href="https://github.com/danieltanfh95/continuity-auth">Continuity Auth</a>: treat all newcomers as suspect, and identify them via patterns, not names. This is Chaum’s 1985 thesis applied to the rate-limiter (security without identification), instead of the attester model his blind-signature lineage grew into (Privacy Pass, Apple’s Private Access Tokens), where a third party vouches for you. Here there is no attester and no web-of-trust to concentrate power in a mega-corporation: trust is built directly between you and the service by behaving consistently over time.</p><p>More importantly, this raises the cost of bot farms by making Sybil attacks uneconomical: genuine users (human or AI) engaging in good faith will happily wait for time to pass, while bot farms must maximise value-per-compute or they lose money, so “sit still and behave for two weeks” is the one cost they can’t parallelize away.</p><p>In continuity-auth, I provide a multi-tier model: the more identity you provide, the higher the initial trust afforded, but you still join a time-gated suspect test until you are time-proven to be a good actor.</p><p>The trust signal itself borrows from my exploration in <a href="https://danieltan.weblog.lol/2026/04/time-flies-in-the-ai-world-my-agent-lineage-evolution-ale-framework-in-2025-was">succession</a> of how human memory “actually works”. We treat historical writings with much more weight than what has happened recently, especially when said writings have recent analogues. Instead of rewarding whoever shows up the most, we reward whoever stayed the longest, by weighing spaced recurrence over massed frequency. Volume is cheap to manufacture, calendar time is not, which is exactly the asymmetry a bot farm cannot buy its way out of.</p><p>Real alignment begins between humans and LLMs when we build for good actors, not make grand claims that are time-proven to be wrong, time and time again.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3e9ada8d2bfd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Minimal Unity Vertical Scroll Rect Setup]]></title>
            <link>https://medium.com/@danieltanfh95/minimal-unity-vertical-scroll-rect-setup-92065c684ce1?source=rss-6f800b737df0------2</link>
            <guid isPermaLink="false">https://medium.com/p/92065c684ce1</guid>
            <category><![CDATA[unity3d]]></category>
            <category><![CDATA[layout]]></category>
            <dc:creator><![CDATA[Daniel Tan]]></dc:creator>
            <pubDate>Sun, 06 Aug 2023 06:34:41 GMT</pubDate>
            <atom:updated>2023-08-06T06:34:41.923Z</atom:updated>
            <content:encoded><![CDATA[<p>The default Unity scroll rect is usually not what you want, but setting up your own scroll rect is quite cumbersome when you forgot some detail causing your scroll rect now to work.</p><p>This guide is also my own reference for setting up a working scroll rect in Unity3D.</p><h3>Concepts</h3><p>These UI concepts are needed because Unity doesn’t provide some defaults (like what we’re used to in browsers and apps).</p><h4>Masking aka ViewPort</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/451/1*2smslsAVmEhM2_N_wLouow.png" /></figure><p>Masking is used to show a portion of the actual content. Since the content you’re showing is likely to be longer than the phone screen itself, you will need to tell Unity how much content you want to show to the player.</p><h4>Size Fitting</h4><p>Unity UI elements don’t expand to fit the size of their children by default, so you need to use Content Size Fitter.</p><p>There are three options you can have for both horizontal and vertial axes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/888/1*d_DeT68kQFzJnUxpmBL8MQ.png" /></figure><ol><li>Unconstrainted: Do-nothing. Use this as the default when you want one axes to fit but not the other.</li><li>Min Size: Fit child elements only after a certain size.</li><li>Preferred Size: Fit child elements.</li></ol><p>Size fitting happens dynamically so you need to hit Play to see how it actually looks like.</p><h3>Setup</h3><p>Create three game objects in this hierarchy.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/282/1*pDM_7euHDMh9vXX2b5_iKQ.png" /></figure><p>Components:</p><ol><li><strong>scroll_rect</strong></li></ol><p>a) Scroll Rect (required), set View Port and Content to viewport and content respectively.</p><p>b) Image (optional), for debugging or background.</p><p>Any size is fine. What is important is the viewport and content size.</p><p>2. <strong>viewport</strong></p><p>a) Image (required), this serves as a cue for unity to know what the masking area is.</p><p>b) Mask (required), this tells Unity to use the Image component as the area for masking. You can hide the mask graphic here.</p><p>Set this to whatever size you want the player to see. I usually anchor this to stretch with the scroll_rect.</p><p>3. <strong>content</strong></p><p>Anchor this to follow the Start Corner and Child Alignment setting.</p><p>a) Any Layout Group works.</p><p>b) Add a Content Size Fitter. If you use a Vertical Layout Group, set Vertical Fit to preferred size etc.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=92065c684ce1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Storm Troopers Guide to Navigating Large Legacy Codebases]]></title>
            <link>https://medium.com/@danieltanfh95/a-storm-troopers-guide-to-navigating-large-legacy-codebases-dc8f4f43f740?source=rss-6f800b737df0------2</link>
            <guid isPermaLink="false">https://medium.com/p/dc8f4f43f740</guid>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Daniel Tan]]></dc:creator>
            <pubDate>Thu, 06 Apr 2023 13:07:13 GMT</pubDate>
            <atom:updated>2023-04-06T13:07:13.249Z</atom:updated>
            <content:encoded><![CDATA[<h4>What to do when you’re dropped into presumed-hostile territory</h4><h3>The checklist</h3><p>1. Understand the land<br>2. Move with a goal<br>3. Leave it better than you found it</p><h3>Understand the land</h3><p>When approaching a legacy codebase, the first thing to do is scour as much information available possible. Skim through the documentation, have casual chats with people inside and outside the team to find out the following:</p><p>1. What business verticals are there?<br>2. How do they rank in priority for the company?<br>3. Which one do you or your team support?<br>4. How is it supported?<br>5. What impedes you or your team from doing so?</p><p>Most guides advising how to navigate large codebases like <a href="https://www.jeremyong.com/game%20engines/2023/01/25/grokking-big-unfamiliar-codebases/">this one</a>, <a href="https://understandlegacycode.com/blog/dive-into-an-unfamiliar-codebase-from-its-edges/">this one</a> or <a href="https://www.quora.com/What-are-good-ways-to-rapidly-become-familiar-with-a-large-codebase-What-are-critical-patterns-to-hunt-for-first-Are-there-tools-that-make-this-easier-or-more-efficient">this one</a> often approach it from a technical perspective, but in my years of being orbital-dropped into legacy codebases in the backlines, my experience has been that the technicals are often the easiest to grok. What trips people is the weird business jargon, the overlapping, nonsensical business rules and the crazy architecture created to solve problems that may or may not exist. Finding the answers to the list above has helped me cut through the cruft quickly and efficiently by allowing me to build a mental model of what’s core, what’s not, relative to my area of work.</p><p>It also has the secondary benefit of generating a political landscape graph of who is in charge of what. You can’t hope to remember everything that goes on in the application, especially when you’ve just started, so you need to find out who to ask when you need it.</p><p>After building an understanding of the land, you need a glossary to navigate it. A glossary is a map of the land. It may or may not exist. If it does not, you need build one.</p><h3>Move with a goal</h3><p>When wading through a codebase it’s easy to get lost in the code and enter a state where you simultaneously understand and not understand what’s going on in the codebase. Stuff like build steps, configurations, setting up a development environment and debugging anything that goes wrong is easier when you have a purpose in mind instead of mindlessly plouging through the code.</p><p>Try asking for a small task with someone to pair program with, and be prepare to spend 2x-4x the time needed compared to others in the same team on it while you go through the codebase and figure out how to:</p><p>1. Configure and setup an environment for debugging and writing tests. <br>2. Identify dead code and unused code paths.<br>3. Figure out where and how data flows through the application.</p><p>Large codebases often comes up technical debt. While it is often good practice to clean up debt as soon as possible, business requirements often mean this is not possible. Instead, people learn to navigate around debt, and as a newcomer, these domain knowledge is not immediately apparent to you. Some challenges around this is duplicated code and defensive programming against team members.</p><h3>Leave it better than you found it</h3><p>It’s always easy to rant about how bad things are, but it’s more productive to use your experience and knowledge to provide a better solution whenever possible.</p><p>Providing documentation, adding comments, drawing graphs, fixing minor issues go a long way in improving the development experience for yourself and your team.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dc8f4f43f740" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Lessons Learnt From Shipping An AI Application]]></title>
            <link>https://medium.com/ai-innovation/lessons-learnt-from-shipping-an-ai-application-ed7cdccdfcf8?source=rss-6f800b737df0------2</link>
            <guid isPermaLink="false">https://medium.com/p/ed7cdccdfcf8</guid>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[learning-and-development]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[mlops]]></category>
            <dc:creator><![CDATA[Daniel Tan]]></dc:creator>
            <pubDate>Sat, 28 Nov 2020 13:27:57 GMT</pubDate>
            <atom:updated>2020-11-28T13:27:57.118Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n22oOEyMFRdeIoYxvzfFKg.jpeg" /></figure><p>This is part of the MLOps series.</p><p>We finally launched a limited public beta for our Autopaint tool, <a href="https://creart.innovrepublic.com/">https://creart.innovrepublic.com/</a> , and these are lessons learnt during the past few months of development. Some of them might seems weird, but bear with me.</p><p>TLDR:</p><ol><li>Don’t skip design sprints.</li><li>Treat AI deployments as independent deployments</li><li>Enable non-developers to edit static configuration of your application.</li><li>Mirror the deployment setup as close as production</li></ol><h3>Don’t Skip Design Sprints</h3><p>Typically developers like to take a bunch of requirements and begin hacking away. This is the advice to stop and think more about the application before beginning, especially for an AI application. Most products probably don’t need “machine learning” or “artificial intelligence” as their core technology, because all they really need is some good statistics algorithm (discounting how AI/ML can bring some good marketing, but that’s a business decision). However, if your application uses AI/ML as the core, then you probably want to take a step back and understand what does your users want from AI/ML, and how would they be using it.</p><p>An unused product feature is good as dead.</p><p>Users typically want to use AI/ML for any of the following reasons (<a href="https://www.ciosummits.com/what-consumers-really-think-about-ai.pdf">check out this report</a>):</p><ol><li>The product can learn. (57% of people surveyed thinks AI can do this)</li><li>The product can replicate human interaction and interpret speech (35% and 37% respectively)</li></ol><p>So, if your product claims to have AI, and it misses the baseline of what people think AI can do, you’re going to disappoint your users. By then, you might as well say your product has a proprietary algorithm and users will be none the wiser (Looking at you Uber).</p><p>As such, you probably want to reflect that in your product design, including UI/UX. You probably want to provide and highlight some of the following in your application:</p><ol><li>The ability to replicate human results (approximated results).</li><li>The ability to learn (error correction).</li></ol><p>Non-AI applications typically don’t provide this because their results typically carry a low risk factor. When was the last time you expected a calculator to fail? And yet AI applications will fail, because they only provide a good approximation of the results the users want. As such AI applications should be designed as a <strong>loop</strong>, instead of a straight line.</p><p>This is where the design sprints come in, to provide some buffer for the development (including designers, artists and techies) team to think about how their program works.</p><h3>Treat AI Deployments As Independent Deployments</h3><p>Now the typical SDLC goes like this: planning→analysis→design→implementation→maintenance.</p><p>Even though it’s supposedly a cycle, maintenance doesn’t always go to planning. Once a program hits maintenance, it more or less stays there, and a new version of the program restarts the process as a new process.</p><p>As for AI deployments, that’s usually not the case. During the development of AI applications I drew from my experience in game development where there’s some stuff that’s different from the usual SDLC:</p><ol><li>Artists and game designers have their own development cycle, just like AI scientists, and it is separate from the tech cycle. This means that you actually have to plan your time accordingly to stagger between artists and game designers. This also means there are constant mini-cycles of iteration and communication going on instead of one big cycle with clear, designed stages.</li><li>The maintenance step is a misnomer because you have to constantly monitor for user behaviour to ensure that your application is doing what you think it should. Just like how you can’t expect players to play according to what you think the game is capable of, you should never expect the AI to produce results the users expect. This means your deployment must be capable of being patched outside of the tech cycle, and at arbitrary short times (Remember the racist AI bot?). For games, it’s stuff like asset bundles to update game mechanics and art, and for AI it is splitting model deployment and code deployment.</li></ol><h3>Enable Others To Edit Static Configuration Of The Application</h3><p>AI engineers are usually focused on making sure their models run well, and AI development is never-ending. They are always in development mode, and the line between production and development is pretty thin. In addition to that, most AI engineers are Python based, so you can’t expect them to understand Golang code or whatever.</p><p>This is fine and dandy because most AI applications are micro-service based architectures. So teams don’t usually come into conflict… until they do, because the AI engineer is likely the domain expert, and they will need to tweak some variables of your application. During development of our application, we began to understand just how much needs to be tweaked, especially since AI is the core of our application.</p><p>As such, we abstracted out configuration so that our AI engineers can tweak JSON files to what they needed, without asking the developers for help. This helped us immensely, especially when the developers were busy fixing bugs during a tight sprint while the models are trained for deployment, so the AI engineers can come fix some of the smaller items just by changing the configuration.</p><p>The same happened when it comes to deploying the applications to different environments. The AI engineer abstracted the file paths for models and other micro-services to some JSON file, and the operations person just needed to maintain different configuration for different environments without bothering too much of the core code.</p><h3>Mirror The Deployment Setup As Close As Production</h3><p>We chose to use Kubernetes via <a href="https://microk8s.io/">Microk8s</a> as our core method of deployment as it paid off greatly when we moved from Aliyun to Azure. It took us a total of a week to get everything setup as it should, and by the end of the week everything was working as it should without a hitch, save for a gotcha where Azure doesn’t actually mount your new disk for you (<a href="https://docs.microsoft.com/en-us/azure/virtual-machines/linux/attach-disk-portal">https://docs.microsoft.com/en-us/azure/virtual-machines/linux/attach-disk-portal</a>)</p><p>I created my own templating tool for managing environments (<a href="https://github.com/danieltanfh95/clojube">https://github.com/danieltanfh95/clojube</a>) but you’re also welcome to use Kustomize or Helm. In general this is what we did to achieve this:</p><ol><li>Containerization. This allowed us to prevent “it works on my machine” sort of problems. You especially don’t want to deal with issues like this during a tight deployment schedule.</li><li>Decentralized, versioned configuration for K8s. Kubernetes allows you to save your deployment as YAML files and it’s very powerful, because now you can tie your deployment with your code version! This means given some kubernetes cluster you can ensure that for a particular version of your application the cluster will be setup exactly as you intended for that version.</li><li>Decentralized CI/CD configuration. We used Gitlab for this. This allowed to build and maintain separate docker containers of our application, specific to some environment. This is especially important because you may have multiple production environments and they might need some change in configuration in some way. Doing this at the CI/CD level allows your developers to focus on getting features out instead of trying to fit their code to the environment. Also, gitlab-runners are decentralized, so it means we can setup some configured GPU machine will build our models that the AI engineers can also change.</li></ol><p>So there you go, some unconventional learnings from developing and deploying an AI application for public use.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ed7cdccdfcf8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ai-innovation/lessons-learnt-from-shipping-an-ai-application-ed7cdccdfcf8">Lessons Learnt From Shipping An AI Application</a> was originally published in <a href="https://medium.com/ai-innovation">AI2 Labs</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Pragmatic Programming Understanding]]></title>
            <link>https://medium.com/glassblade/pragmatic-programming-understanding-ec70156bcfbb?source=rss-6f800b737df0------2</link>
            <guid isPermaLink="false">https://medium.com/p/ec70156bcfbb</guid>
            <category><![CDATA[beginner]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Daniel Tan]]></dc:creator>
            <pubDate>Fri, 18 Sep 2020 10:25:49 GMT</pubDate>
            <atom:updated>2020-09-18T10:28:03.505Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*YqiubKNXJajAx-tg" /></figure><p>I’m taking a break from my usual articles to write something for beginners starting to program: Brief, simple explanations of various concepts and programming terms. These came from explaining some concepts to my friend who was completing a curriculum in computer science.</p><h4>Features vs Design vs Implementation</h4><ol><li>Features: What the program can do</li><li>Implementation: How the program does it</li><li>Design: Planning features and implementations.</li></ol><h4>Programming paradigms</h4><p>In programming we have logic (how), and data (what). Do note that these paradigms can be mixed.</p><p>Functional paradigm: It’s when you pass in data into logic, and have a new data as a result. E.g. Clojure.</p><p>Object Oriented paradigm: It’s when you place logic in data, and data uses signals to communicate between each other on what logic to trigger. E.g. Ruby</p><p>There are also paradigms that focus on what to control, the logical command or the logical result.</p><p>Imperative paradigm: You make logical commands that describe logic to change your data. E.g. C.</p><p>Declarative paradigm: You explain the final data result to program and it solves the commands to do it for you.</p><h4>Types</h4><p>Untyped languages are like maths. You focus on getting the algorithms right. Here we use schemas to restrict the range of data that can be correct. E.g. square root, but following schema of positive integers. This includes Python and Ruby.</p><p>Typed languages are like physics, you focus on getting the results right. Here types perform basic checking on intermixed values. For more complex checking to the level of schemas you need dependent types. This includes C and Haskell.</p><h4>Mutability</h4><p>Mutable means “can be changed. Immutability, the opposite, can prevent more human errors by making sure data can only be copied, but not changed. However, the trade off is that your program takes more space and is slower.</p><p>On the other hand, programs using mutable data structures can be very fast and use less memory but programmers will make more mistakes.</p><h4>Project management</h4><p>Scope: How much will you complete</p><p>Resources: How much can you use (including human resources)</p><p>Time: How long will you take</p><p>This brings us to the development styles:</p><p>Waterfall: Fixed scope, but flexible resources and time.</p><p>Agile: Fixed resources and time but flexible scope.</p><h4>Spectrum of development</h4><p>All of these require algorithm and data structure knowledge.</p><p>Front-end: User-facing application where you have less control over the environment. Can be web based, like HTML/JS/CSS, or native, like C++ or Java.</p><p>Back-end: Non-user-facing application where you have more control over the environment. Any language can be a backend language.</p><p>Infrastructure and Operations: Applications that host the front-end or back-end applications, and how to transfer the front-end application to the user etc</p><p>Data engineering: Applications that gather and clean data for use in other applications like AI applications.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ec70156bcfbb" width="1" height="1" alt=""><hr><p><a href="https://medium.com/glassblade/pragmatic-programming-understanding-ec70156bcfbb">Pragmatic Programming Understanding</a> was originally published in <a href="https://medium.com/glassblade">GlassBlade</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[MLOps: Deploying Aliyun GPU sharing in Kubernetes]]></title>
            <link>https://medium.com/ai-innovation/mlops-deploying-aliyun-gpu-sharing-in-kubernetes-4bc695300c77?source=rss-6f800b737df0------2</link>
            <guid isPermaLink="false">https://medium.com/p/4bc695300c77</guid>
            <category><![CDATA[mlops]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[gpu]]></category>
            <dc:creator><![CDATA[Daniel Tan]]></dc:creator>
            <pubDate>Thu, 27 Aug 2020 03:31:13 GMT</pubDate>
            <atom:updated>2020-10-20T03:09:01.888Z</atom:updated>
            <content:encoded><![CDATA[<p>This article is part of the MLOps series.</p><p>Since GPU machines are extremely expensive, we would want to cram as much applications as possible. However, with the current Nvidia device plugin, which only allows you to specify how many GPUs you want to use, obviously that is not ideal, since most AI applications use about less than half of the GPU memory available. This is an obvious bottleneck in deploying AI applications for most companies. We probably can expect more innovation in this area of technology for efficient use of GPU resources, but we do have the next best thing right now.</p><p>Alibaba open sourced their <a href="https://github.com/AliyunContainerService/gpushare-scheduler-extender">GPU scheduler</a> and device plugin that also exposes the amount of GPU memory in your cluster. This way, you can also say how much GPU memory you want to use, allowing us to run multiple AI programs on the same GPU. While there has been push for algorithms that can run on the CPU, with the advent of 5G I can see a need to push for algorithms that can process HD videos, which would require GPUs to be effective.</p><h3>Installation</h3><h3>Nvidia</h3><p>First of all, the docker engines must support using the nvidia docker engine.</p><p>Install nvidia-docker2 (1 has been discontinued)</p><pre>curl -s -L &lt;https://nvidia.github.io/nvidia-docker/gpgkey&gt; | \\<br>sudo apt-key add -<br>distribution=$(. /etc/os-release;echo $ID$VERSION_ID)<br>curl -s -L &lt;https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list&gt; | \\<br>sudo tee /etc/apt/sources.list.d/nvidia-docker.list<br>sudo apt update &amp;&amp; apt install nvidia-docker2 -y</pre><p>Change /etc/docker/daemon.json to use the following, before restarting docker.</p><pre>{<br>	&quot;default-runtime&quot;: &quot;nvidia&quot;,<br>	&quot;runtimes&quot;: {<br>		&quot;nvidia&quot;: {<br>				&quot;path&quot;: &quot;/usr/bin/nvidia-container-runtime&quot;,<br>				&quot;runtimeArgs&quot;: []<br>			}<br>		}<br>}</pre><h3>The plugin</h3><p>Installing the plugin is pretty tricky. First of all you need to add a file to the kubernetes installation (this article assumes kubeadm is used.)</p><pre>cd /etc/kubernetes/<br>sudo curl -O &lt;https://raw.githubusercontent.com/AliyunContainerService/gpushare-scheduler-extender/master/config/scheduler-policy-config.json&gt;<br>kubectl create -f &lt;https://raw.githubusercontent.com/AliyunContainerService/gpushare-scheduler-extender/master/config/gpushare-schd-extender.yaml&gt;</pre><p>Then you’ll need to modify the default scheduler configuration in /etc/kubernetes/manifests/kube-scheduler.yaml</p><p>Add the new policy configuration that you’ve downloaded to the original scheduler arguments.</p><pre>- --policy-config-file=/etc/kubernetes/scheduler-policy-config.json</pre><p>Add volume mounts to the pod specification to access the configuration. This should point to wherever you’ve downloaded the json file.</p><pre>- mountPath: /etc/kubernetes/scheduler-policy-config.json<br>  name: scheduler-policy-config<br>  readOnly: true</pre><pre>- hostPath:<br>      path: /etc/kubernetes/scheduler-policy-config.json<br>      type: FileOrCreate<br>  name: scheduler-policy-config</pre><p>Since kubernetes has likely deployed the scheduler as a static pod, we need to copy and paste the file to trigger the change like so.</p><pre>sudo cp /etc/kubernetes/manifests/kube-scheduler.yaml /tmp <br>sudo cp /tmp/kube-scheduler.yaml /etc/kubernetes/manifests/kube-scheduler.yaml</pre><p>Then now finally, we can deploy the actual gpu device plugin. Make sure not to use the original NVIDIA plugin.</p><pre>kubectl create -f &lt;https://raw.githubusercontent.com/AliyunContainerService/gpushare-device-plugin/master/device-plugin-rbac.yaml&gt;<br>kubectl create -f &lt;https://raw.githubusercontent.com/AliyunContainerService/gpushare-device-plugin/master/device-plugin-ds.yaml&gt;</pre><p>We all need to mark any node we want to use this plugin with a new label.</p><pre>kubectl label node &lt;target_node&gt; gpushare=true</pre><p>Finally, you should also install the inspection extension for kubectl wherever you install kubectl.</p><pre>cd /usr/bin/<br>sudo wget &lt;https://github.com/AliyunContainerService/gpushare-device-plugin/releases/download/v0.3.0/kubectl-inspect-gpushare&gt;<br>sudo chmod u+x /usr/bin/kubectl-inspect-gpushare</pre><p>And voila you can now share gpu memory among your kubernetes applications!</p><p>Do note however, that tensorflow applications take up all GPU memory by default, you will need to set the memory to grow as the application is used <a href="https://www.tensorflow.org/guide/gpu">as shown here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4bc695300c77" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ai-innovation/mlops-deploying-aliyun-gpu-sharing-in-kubernetes-4bc695300c77">MLOps: Deploying Aliyun GPU sharing in Kubernetes</a> was originally published in <a href="https://medium.com/ai-innovation">AI2 Labs</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using Docker Images for Cloud Native Artifacts]]></title>
            <link>https://medium.com/ai-innovation/using-docker-images-for-cloud-native-artifacts-c1a3d81c24b2?source=rss-6f800b737df0------2</link>
            <guid isPermaLink="false">https://medium.com/p/c1a3d81c24b2</guid>
            <category><![CDATA[cloud-native]]></category>
            <category><![CDATA[cloud]]></category>
            <category><![CDATA[mlops]]></category>
            <category><![CDATA[kubernetes]]></category>
            <dc:creator><![CDATA[Daniel Tan]]></dc:creator>
            <pubDate>Sun, 16 Aug 2020 03:05:01 GMT</pubDate>
            <atom:updated>2020-10-20T03:08:46.862Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/481/1*Idc60NrqIWIoannyzVL_zQ.png" /></figure><p>Continuing from <a href="https://medium.com/glassblade/pragmatic-k8s-understanding-2cd45a291cf5">my previous article on Kubernetes</a>, this article will focus on showing how we can use docker images to host artifacts in the form of files.</p><h3>Why?</h3><p>Typically, most software applications blur the line between code and assets. When scaling up, we simply duplicate whatever grouping we have of the application. However this is not ideal for applications dealing with artifacts that has its own independent lifecycle for development and release, such as AI/ML models. This is especially applied to cases where these files can get pretty heavy in resource strapped situations such as scaling for GPU-based solutions where the price to autoscale recklessly is prohibitive.</p><p>Beside resource constraints and dealing with independent lifecycles, we can also include the artifacts build process into the CI/CD process that we already have with other software products that are using docker etc, thus reaping the benefits of automation.</p><p>This is not something radical, and has been explored by Microsoft with their open source tool : <a href="https://github.com/deislabs/oras#oras-background">ORAS</a>. However, we can simply use Docker as well.</p><h3>How?</h3><p>There are two steps to this. Packaging and deployment.</p><h3>Packaging</h3><p>The docker file for this will be slightly different as compared to the usual. While the final artefacts can be merged into one container, you should try to copy them over using multi-stage builds so that you can take advantage of layer caching. This will reduce the amount of repeated copying of the files needed in the future when it is rebuilt.</p><p>Your artefacts can be generated using whatever pipeline your team has already been using this way.</p><pre>FROM alpine:latest AS model1<br>COPY ./densenet_model.pb .</pre><pre>FROM alpine:latest AS model2<br>COPY ./densenet_model_2.pb .</pre><pre>FROM alpine:latest<br>WORKDIR /data<br>COPY --from=model1 /densenet_model.pb .<br>COPY --from=model2 /densenet_model_2.pb .</pre><p>What I also use is gitlab CI/CD, because the gitlab runner can be installed on the specific machine your team has been using to generate their models.</p><p>In this case all you need to do in your docker gitlab runner is to change the following in config.toml:</p><pre>...<br>[[runners]]<br>  ...<br>  [runners.docker]<br>    ...<br>    privileged = true<br>		...<br>    volumes = [&quot;/cache&quot;, &quot;&lt;path in your host&gt;:&lt;path in your container&gt;:ro&quot;]</pre><p>And in your .gitlab-ci.yml you can access the artefacts from the path in your container specified in the config/toml file.</p><p>Once you’ve built and pushed your docker image, you now have your artefacts in the cloud.</p><h3>Deployment</h3><h3>Docker-only</h3><p>Now, if you’re deploying only using docker, you can run it like so</p><pre>docker volume create shared-volume<br>docker run -v &lt;path in container&gt;:shared-volume:ro artefact-image<br>docker run -v shared-volume:&lt;path in container&gt;:ro app-image</pre><p>Any container which has mounted the volume will automatically share the artefacts, which is pretty neat.</p><h3>Kubernetes (K8s)</h3><p>K8s doesn’t really recommend host storage. However, we want to use it so that we can take advantage of linux memory page caching, which would ensure that applications on the same computer will reuse the same pointer to the file. This will avoid the situation where multiple containers using the same file blows the memory available when scaling up.</p><p>We can still do it, somehow. I will only show the example YAML files here. It should be a simple matter of doing kubectl apply -f &lt;file name&gt;.yaml</p><p>First of all, we need to create the K8s volume and the volume claim. You should create one on each host.</p><pre>apiVersion: v1<br>kind: PersistentVolume<br>metadata:<br>  name: example-pv<br>  labels:<br>    type: local<br>spec:<br>  storageClassName: manual<br>  capacity:<br>    storage: 1GiB<br>  accessModes:<br>    - ReadOnlyMany<br>  hostPath:<br>    path: &quot;/tmp/data&quot;</pre><pre>---</pre><pre>apiVersion: v1<br>kind: PersistentVolumeClaim<br>metadata:<br>  name: example-pvc<br>spec:<br>  storageClassName: manual<br>  accessModes:<br>    - ReadOnlyMany<br>  resources:<br>    requests:<br>      storage: 1GiB</pre><p>Then we create a DaemonSet, which is basically a deployment that will run once on each machine in the cluster. This will make sure each machine will have the files copied once each time it is added to the cluster.</p><pre>apiVersion: apps/v1<br>kind: DaemonSet<br>metadata:<br>  name: example<br>spec:<br>  selector:<br>    matchLabels:<br>      name: example<br>  template:<br>    metadata:<br>      labels:<br>        name: example<br>    spec:<br>      volumes:<br>      - name: example<br>        hostPath:<br>          path: &lt;path on host&gt;<br>          type: Directory<br>      containers:<br>      - name: first<br>        image: &lt;artefact-image&gt;<br>        volumeMounts:<br>        - name: example<br>          mountPath: &lt;path on container&gt;<br>        command: [&quot;/bin/sh&quot;, &quot;-c&quot;]<br>        args:<br>          - &quot;mkdir -p &lt;sub path on host&gt;; cp &lt;your files&gt;&quot;</pre><p>Note that since the volumes in k8s is initially one way (host files will be linked into the container, but not the other way around, so your files in the container will be overwritten), you can use cp to populate the host instead.</p><p>Now you can use it in your pods like in the following example:</p><pre>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: example<br>spec:<br>  volumes:<br>    - name: example-storage<br>      persistentVolumeClaim:<br>        claimName: example-pvc<br>  containers:<br>    - name: example<br>      image: &lt;your image&gt;<br>      volumeMounts:<br>        - mountPath: &lt;path in container&gt;<br>          name: example-storage</pre><p>And there you have it, “artifact-sharing” using docker images.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c1a3d81c24b2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ai-innovation/using-docker-images-for-cloud-native-artifacts-c1a3d81c24b2">Using Docker Images for Cloud Native Artifacts</a> was originally published in <a href="https://medium.com/ai-innovation">AI2 Labs</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Pragmatic K8s Understanding]]></title>
            <link>https://medium.com/ai-innovation/pragmatic-k8s-understanding-2cd45a291cf5?source=rss-6f800b737df0------2</link>
            <guid isPermaLink="false">https://medium.com/p/2cd45a291cf5</guid>
            <category><![CDATA[mlops]]></category>
            <category><![CDATA[containers]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[kubernetes]]></category>
            <dc:creator><![CDATA[Daniel Tan]]></dc:creator>
            <pubDate>Sun, 19 Jul 2020 04:42:28 GMT</pubDate>
            <atom:updated>2020-10-20T03:08:13.880Z</atom:updated>
            <content:encoded><![CDATA[<p>This article simplifies k8s as a magical tool to simplify deployment. By the end of the article you should have a good high level understanding of kubernetes concepts.</p><p>This article is also part of a Machine Learning Ops (MLOps) series.</p><p>Before we start, the image below describes the experience of learning k8s right now in spite of the resources widely available in the web, which prompted me to write an article that follows the <a href="https://en.wikipedia.org/wiki/KISS_principle">KISS principle</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/695/0*5iqAZaUAZSev_2Nm.jpg" /></figure><h3>Why kubernetes (k8s)?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*mcn_dC3qSZ5mkFX7" /></figure><p>Remember when during development you only have one machine to deploy with, and you’re asked to scale your application without much budget? Naturally, you set up a load balancer and create two instances with different ports to handle the extra load. Later on, you’re given more computers, and you do the same thing on those computers as well. This becomes a tedious task as more and more computers are required, and it’s still not an easy task even with tools like Ansible.</p><p>What k8s does is automating away all of this, so most of the times, after you set up k8s, all you need to do is register your new computer with labels of apps that are allowed on it, and k8s takes care of the rest. On a single computer, it handles load balancing and ports automatically for your applications when scaling up, all without bundles of bash/Ansible scripts, which brings us to the next great feature of k8s.</p><p>Writing k8s configuration files are pretty much like writing SQL code whereas tools like Ansible are similar to writing Python code. You just describe what you want, and k8s finds the best way to do it. Since the configuration files are just YAML files, you can keep it with your repository as well. Updating the deployment is as easy as updating the configuration files and applying it to the cluster.</p><p>This means you can bring your application to different clusters in different environment and expect more or less similar behaviour, which makes it very attractive for a lot of companies.</p><p>However, k8s requires containers to do its magic, so before I proceed to provide a simple guide to k8s, I have to explain containers.</p><h3>Why containers?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/425/0*QXZveqr1zRs5Zo1Z" /></figure><p>Nuff’ said.</p><h3>The Actual Guide</h3><h4>K8s terms translator</h4><ol><li>Node: Computer that is not a container.</li><li>Cluster: Group of nodes.</li><li>Control plane: Node that controls other nodes.</li><li>Persistent Volumes: A description of what storage is available.</li><li>Persistent Volume Claims: A description of what storage you need.</li><li>Pods: A description for how containers are run and how they use persistent volumes.</li><li>Deployment: A group of pods.</li><li>Service: A description for how deployments communicate through the internet.</li></ol><h4>Using k8s</h4><ol><li><a href="https://minikube.sigs.k8s.io/docs/start/">Minikube</a> has, by far, the easiest way to start with k8s on a single computer.</li><li>Otherwise, get one from Azure, AWS or GKE.</li><li>Finally it would be setting up a cluster manually using <a href="https://github.com/kubernetes/kubeadm">kubeadm</a> or <a href="https://kubespray.io/#/">kubespray</a>.</li></ol><h3>K8s essentials for developers</h3><p>There’s really four basic concepts you need to know before using k8s: containers, persistent storage, deployments, services.</p><p>We’ll use Nginx for this example, and I will only show the YAML files, since to use it you just save the YAML file and use kubectl apply -f .</p><h4>Containers (Docker)</h4><p>You’ll have to be able to create a docker image. I wrote an article on how to <a href="https://medium.com/glassblade/strategies-for-reducing-docker-image-size-with-python-flask-feef86a63349?source=your_stories_page---------------------------">reduce docker container size</a> that would be useful as well, which also contains a sample docker file used to build the image.</p><p>Do note that containers can be restarted or destroyed often, for example when your container crashes k8s will automatically restart it.</p><h4>Persistent Storage</h4><p>The easiest to start with local data, but there are <a href="https://kubernetes.io/docs/concepts/storage/storage-classes/">many other types as well</a>. This type of storage will not be destroyed when the container is destroyed.</p><pre>apiVersion: v1<br>kind: PersistentVolume<br>metadata:<br>  name: nginx-pv<br>  labels:<br>    type: local<br>spec:<br>  storageClassName: manual<br>  capacity:<br>    storage: 10Gi<br>  accessModes:<br>    - ReadWriteMany<br>  hostPath:<br>    path: &quot;/var/lib/path-in-node&quot;</pre><p>A persistent volume only describes what storage is available, it does not actually allocate the storage or block it off.</p><p>You can use access modes to describe the behaviour of pods when dealing with this storage (i.e. mounting). <em>This is irrespective of the actual file system</em>.</p><p>There are three modes:</p><ul><li>ReadWriteOnce : the volume can be mounted as read-write by a single node</li><li>ReadOnlyMany : the volume can be mounted read-only by many nodes</li><li>ReadWriteMany : the volume can be mounted as read-write by many nodes</li></ul><h4>Deployments</h4><p>This describes an actual “application”. It uses containers and persistent storage.</p><pre>apiVersion: apps/v1<br>kind: Deployment<br>metadata:<br>  name: nginx-deployment<br>  labels:<br>    app: nginx<br>spec:<br>  replicas: 1<br>  selector:<br>    matchLabels:<br>      app: nginx<br>  template:<br>    metadata:<br>      labels:<br>        app: nginx<br>    spec:<br>      volumes:<br>      - name: nginx-vol<br>        persistentVolumeClaim:<br>          claimName: nginx-pvc<br>      containers:<br>      - name: nginx<br>        image: nginx:latest<br>        volumeMounts:<br>        - name: nginx-vol<br>          mountPath: /var/lib/path-in-nginx-container<br>        ports:<br>        - containerPort: 80</pre><p>As noted, you can start with multiple replicas of your application. The deployment will create multiple copies of your pods using the template.</p><h4>Services</h4><p>You can expose your app to the internet using services. You can choose a load balancer between multiple nodes or just use a port on a node.</p><pre>apiVersion: v1<br>kind: Service<br>metadata:<br>  name: nginx-service<br>spec:<br>  selector:<br>    app: nginx<br>  ports:<br>    - protocol: TCP<br>      port: 80<br>      nodePort: 30080<br>  type: NodePort</pre><p>The service automatically load balances between pods created by a deployment.</p><p>The service selects deployment based on the app label specified in the deployment (i.e. the selector).</p><p>Services, by default are only allowed to use ports 30000–32767. You can use another reverse proxy or ingress to do port forwarding, common options are Nginx and HAProxy.</p><h3>Conclusion</h3><p>And that’s basically most of what you need to actually be productive in k8s.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2cd45a291cf5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ai-innovation/pragmatic-k8s-understanding-2cd45a291cf5">Pragmatic K8s Understanding</a> was originally published in <a href="https://medium.com/ai-innovation">AI2 Labs</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Strategies for Reducing Docker Image Size, with Python Flask]]></title>
            <link>https://medium.com/ai-innovation/strategies-for-reducing-docker-image-size-with-python-flask-feef86a63349?source=rss-6f800b737df0------2</link>
            <guid isPermaLink="false">https://medium.com/p/feef86a63349</guid>
            <category><![CDATA[flask]]></category>
            <category><![CDATA[dockerfiles]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Daniel Tan]]></dc:creator>
            <pubDate>Wed, 17 Jun 2020 05:32:30 GMT</pubDate>
            <atom:updated>2020-10-20T03:07:47.859Z</atom:updated>
            <content:encoded><![CDATA[<h3>General Strategy for Reducing Docker Image Size, with Python Flask Example</h3><p>Docker is a nice tool for containerizing our applications, but we often need to take note of the image size which can go up to a few GiBs. This post details general strategies that can be applied to most languages, and I’ll be using Python Flask to showcase how to apply it on a Python, which can lead to up to 90% reduction in image size.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/323/0*I10YzIe264RVQwnI" /></figure><h3>Remove Dead Cruft</h3><p>When transitioning from an exploratory phase to pre-production, try to remove dead code and unused dependencies that are left over from exploration. In agile processes, this can happen quite often, so it is helpful to setup CI or specify a linter like PyFlakes for the team.</p><p>Removing these can help speed up the docker build process as well, since some dependencies might require a compilation step on installing on a fresh system.</p><p>In Python, we can use several tools with this, but the steps are pretty generic and can be applied to any language.</p><h4><strong>1. Remove dead code</strong></h4><h4><strong>Vulture</strong></h4><p><a href="https://github.com/jendrikseipp/vulture">Use vulture to find out if your code has any obvious dead code</a>, leading to unneeded imports. Remove everything you can find.</p><h4>2. Remove unused dependencies</h4><h4>List Imports</h4><p>Next would be reviewing what imports are actually required by our code. list-imports will <a href="https://github.com/andrewp-as-is/list-imports.py">find imports in your code</a>.</p><p>You should put this in a requirements.in or setup.py file, and remove all code-related and standard libraries imports so you keep a list of all top-level imports.</p><h4>Pip-tools</h4><p>Do not use pip freeze to maintain a list of required dependencies, it can be pretty difficult to prune dependencies in the future. Instead, <a href="https://github.com/jazzband/pip-tools">we can compile dependencies</a> using pip-tools.</p><p>You can treat the requirements.txt as a generated file instead, and deal directly with setup.py or requirements.in.</p><h3>Bundling dependencies with your app</h3><p>This step bundles all the required libraries to create a portable app you can move around. The point is to strip away build files and only extract the compiled files that we need to run the app.</p><p>Providing this will also allow us to do the next step, docker multi-stage, which is the largest memory saver.</p><h4>Pyinstaller</h4><p>Pyinstaller works pretty well with flask since it can include our template and static files as well. However, there are one change to be made for flask apps, which is to change how we find the folders for the static directories. <a href="https://pyinstaller.readthedocs.io/en/stable/runtime-information.html#using-file">More information can be found here</a>.</p><pre>if getattr(sys, &#39;frozen&#39;, False):<br>    template_folder = os.path.join(sys._MEIPASS, &#39;templates&#39;)<br>    static_folder = os.path.join(sys._MEIPASS, &#39;static&#39;)<br>    app = Flask(__name__, template_folder=template_folder, static_folder=static_folder)<br>else:<br>    app = Flask(__name__, static_folder=&#39;static&#39;)</pre><p>After that, the command you use to build you app can be like the following example.</p><pre>pyinstaller -w -F \<br>	--add-data &quot;templates:templates&quot; \<br>	--add-data &quot;static:static&quot; \<br>	--hidden-import=&#39;pkg_resources.py2_warn&#39; \<br>	app.py</pre><p>This particular hidden import is needed due to a problem with setup tools &gt; 45.0.0, <a href="https://github.com/pypa/setuptools/issues/1963">as seen here</a>.</p><p>You might need a few tries to get this working since pyinstaller is only able find top level, obvious imports from your code, <a href="https://pyinstaller.readthedocs.io/en/stable/when-things-go-wrong.html?highlight=hidden-import#listing-hidden-imports">more information here</a>:</p><p>You can build the app with --debug=imports and run it to find hidden imports.</p><h3>Docker Multi-Stage Builds</h3><p>Sometimes we might hear that Dockerfile best practices involve putting all your RUN commands into one line to reduce layer caching and reduce image size.</p><p>This is no longer helpful with multi stage builds since we should take advantage of layer caching when it makes sense.</p><p>Example docker file for the purpose of illustrating these steps:</p><pre>FROM python:3.6 AS build-stage<br>RUN apt update &amp;&amp; apt install -y cmake<br>ADD app /<br>RUN pip install -r requirements.txt<br>RUN pyinstaller -w -F \<br>	--add-data &quot;templates:templates&quot; \<br>	--add-data &quot;static:static&quot; \<br>	--hidden-import=&#39;pkg_resources.py2_warn&#39; \<br>	--noconfirm \<br>	app.py</pre><pre>FROM debian:buster-slim AS deploy-stage<br>RUN apt update &amp;&amp; apt install -y libxcb6<br>COPY --from=build-phase /dist/app /app<br>EXPOSE 3001<br>CMD [&quot;/app&quot;]</pre><h4>Picking the right base images</h4><p><strong>Most of your space (and time!) savings actually come from this.</strong></p><p>Since we can split the build and deployment stages, we can pick a heavy image with all the tools we need for the build stage, so we don’t need to do an extra step of downloading and installing. During the deploy stage, we can then choose a minimal, thin image.</p><p>For most applications, this would mean picking whatever thick image like node:latest or python:3.6 and then deploying with a nginx:alpine or a debian:buster-slim image.</p><p>It is interesting that <a href="https://pythonspeed.com/articles/alpine-docker-python/">alpine images are not suitable for python deployments</a>.</p><h4>Layer caching considered helpful</h4><p>You don’t always get the dockerfile right the first time, and it’s helpful for docker not to repeat steps that has been completed before. For that we can leverage layer stage caching. Docker won’t repeat layers and stages that has no change unless a COPY or ADD command invalidates it.</p><p>This means you can explore step by step the correct steps to create your docker image if you split up your RUN, COPY and ADD commands in your build stage correctly by placing COPY and ADD last. However, you still want to keep your commands in the deploy stage to the minimum to reduce image size. You can find more information here : <a href="https://docs.semaphoreci.com/ci-cd-environment/docker-layer-caching/">https://docs.semaphoreci.com/ci-cd-environment/docker-layer-caching/</a></p><p>Stages will not be invalidated unless it has changed.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=feef86a63349" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ai-innovation/strategies-for-reducing-docker-image-size-with-python-flask-feef86a63349">Strategies for Reducing Docker Image Size, with Python Flask</a> was originally published in <a href="https://medium.com/ai-innovation">AI2 Labs</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Pragmatic Monad Understanding]]></title>
            <link>https://medium.com/glassblade/pragmatic-monad-understanding-c6f6447d1bcb?source=rss-6f800b737df0------2</link>
            <guid isPermaLink="false">https://medium.com/p/c6f6447d1bcb</guid>
            <category><![CDATA[clojure]]></category>
            <category><![CDATA[haskell]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[monads]]></category>
            <dc:creator><![CDATA[Daniel Tan]]></dc:creator>
            <pubDate>Wed, 10 Jun 2020 12:20:20 GMT</pubDate>
            <atom:updated>2020-06-11T02:26:41.630Z</atom:updated>
            <content:encoded><![CDATA[<p>This is my personal notes on monads, that some might find helpful. I’ve included more information in the appendix if needed.</p><p>In my experience it is often more useful to talk about what a specific design pattern provides and how it provides it than to talk about what it is, but if you want to play around with the exact definition the monad design pattern has been <a href="https://en.wikipedia.org/wiki/Monad_(functional_programming)">explained</a> to <a href="http://www.tomharding.me/2017/06/05/fantas-eel-and-specification-15/">death</a> on the <a href="https://wiki.haskell.org/Monad">internet</a>. This is my way of understanding the design pattern by breaking it down into simple steps.</p><h3>What does it provide, and how?</h3><p>The monad design pattern allows us to provides a common, easier-to-use API for some context wrapping construct. This is usually implemented through a class or an interface to make it more readable but the notes here use functions for clarity, which will be simple to convert to methods during actual use.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/901/0*m7mSqpmecLjwpIkq" /><figcaption>Ruby example with the Optional monad from <a href="https://www.youtube.com/watch?v=J1jYlPtkrqQ">https://www.youtube.com/watch?v=J1jYlPtkrqQ</a></figcaption></figure><h3>Simple steps</h3><p>The monad design pattern is as follows:</p><p>1. Define some type constructor M where a new type M a can be constructed with any type of a. More information at Appendix [a].<br>Example: Vector or List of type T where T can be anything but this could also be some class or data type.</p><p>2. Define a unit function where it creates a new value with type M a from value with type a.</p><p>Example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nYzMznnMg5epUR6nmE6qGA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VKaxOg_Amf-SVWW55T9yYQ.png" /></figure><p>3. Define a join function that transforms a nested monadic value M (M a) into a value of type M a. More information in appendix[c].</p><p>Example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P1ERVOvHZy5I0lxIPwZCxQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BJxBDdbHKfdLJvSnNlv6Wg.png" /></figure><p>4. Define a fmap function that takes a function, which takes a value of type a and transform it into a value of type b , but does not change the wrapping context M. I.e. mapping over a list of strings into a list of ints, where the wrapping context remains a list.</p><p>Basically here you need to provide some way of interacting with the “inner” value of type a. This usually is the part where it confuses people when dealing with types that are not iterable sequences such as the Maybe type.</p><p>Example: Since the chosen types are iterable sequences, this is simply the usual map function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cLsTS9Jh_fC_yOUnN9QmOw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C-BydJ4Dyf_Lx1n3t2BpSw.png" /></figure><p>5. Define a bind function that takes in a monadic value M a and a callback function that operates on type a to create a new monadic value.</p><p>This bind function is also basically fmap then join , but you can have intermediate steps depending on the type M a.</p><p>Example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r2KLGRExYLY2CiuAP-Xb4A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5n1WplNssfd3JM0OgaRGGA.png" /></figure><h3>So why monads and how does it help you?</h3><p>First of all, it improves readability in languages without using meta-programming support (compared to lisp macros, for example) when there are high levels of nesting and repeated code using the bind function. A good example of this can be found in the <a href="https://en.wikipedia.org/wiki/Monad_(functional_programming)">Wikipedia page for the Maybe Monad</a> and the <a href="https://arrow-kt.io/docs/0.10/patterns/monads/#pattern">kotlin documentation for monads.</a></p><p>Secondly, monads offer multiple ways of sequencing two expressions, since you now can also do conditional callbacks in your bind function, allowing you to use intermediate results from the chain. This helps greatly with handling exceptions and empty values.</p><h3>But why not monads?</h3><p>Most non-FP languages did not use monads as an explicit design pattern because they didn’t need to.</p><p>Imperative languages don’t have this issue of sequencing functions, because they can control when and where to store the results of their function call. This comes with other costs of course.</p><p>OOP languages are similar to imperative languages, besides having the Factory and Service Callback pattern.</p><p>However, monads are slowly being accepted as another design pattern to consider in non-FP languages, even as unlawful monads (see appendix [d]). For example, Promise in Javascript and Optional in Java are unlawful monads, but they implement the interface for monads regardless. This is because monads allows them to specify a common API for classes that might require a lot of boilerplate code to use, with relatively straightforward steps.</p><p>Special mention to lispy programming languages such as Clojure where instead of using an explicit monad pattern, macros allow the user to restructure their code instead of using monads for the two benefits above. For example, the cond macro and threading macros like some-&gt; achieve similar results as using a monad.</p><p>Special thanks to Gabriel Lebec for his help on clarifying concepts and extra details that I’ve also included in the appendix.</p><p>I try to update weekly, but I was segwayed by the post on “<a href="https://news.ycombinator.com/item?id=23454352">The Pain Points of Haskell</a>”.</p><h3>Appendix</h3><p>[a]</p><p>The type constructor M is assumed to follow the Functor design pattern, where it wraps some value in a context, and there is some function fmap that can be applied to the value in the context.</p><p>[b]</p><p>Restrictions for python join and flatten.</p><p>It is possible that the outermost Iterable an inner Iterable might be different types, which is outside of the definition of a monad.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2VzArYFBzzDI5jo3BSj6GA.png" /></figure><p>Also — the way we expect flatten to work in the context of a monad is that every element of the list will itself be a list — i.e., you won’t have a mix of list and non-list values.</p><p>[c]</p><p>For a type constructor to be “classified” as monadic, it has to have the capabilities of functors (map) and applicative functors (unit and ap), plus the &quot;new&quot; bit specific to monads (join, or equivalently bind or kleisli arrow). In terms of implementation, it&#39;s possible to write the function bind before you write the function map , but until you write the map function, it&#39;s just not &quot;complete&quot; as a monad.</p><p>I don’t think there are any types for which you can write a correctly-typed bind but for which you cannot write a corresponding map. If there were such types, I don&#39;t think they could rightly be called monadic.</p><p>Intuitively, this is because it’s “harder” to write bind than map. Also, if you can write bind itself, you can &quot;recover&quot; / define map as bind with unit.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DtJ7gPRo0AP8Bo-sGoh04Q.png" /></figure><p>This is part of why you hear people define monad in terms of bind and unit — together they are powerful enough to define functor map and applicative ap . So you can start at the &quot;end&quot; (monad) and then back-fill the first two steps in the hierarchy.</p><p>[d]</p><p>True monads need to obey the <a href="https://stackoverflow.com/questions/3433608/explanation-of-monad-laws">monadic laws.</a> However, it can still be used without it, but the user has to be careful of some presumptions about the monad.</p><p>Clojure example using functions we’ve defined above:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*N4JUlJBEF4F0UvL565LoAA.png" /></figure><p>Python example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5DVRlYbmilU-HgtH8j6Jpw.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c6f6447d1bcb" width="1" height="1" alt=""><hr><p><a href="https://medium.com/glassblade/pragmatic-monad-understanding-c6f6447d1bcb">Pragmatic Monad Understanding</a> was originally published in <a href="https://medium.com/glassblade">GlassBlade</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>